如何正确实现按钮单击事件以及如何动态更改 GUI 元素



我做了一个小丑陋的GUI,遵循一些教程并阅读我对基本WxPython的理解,我目前正在研究python 2.6。我设法显示大多数内容,但当我单击检查按钮时,我想基本上获取文件选择器按钮上的文件路径,但是当我尝试进行绑定时,我收到一条错误消息,指出对象没有该属性。

在我看来,这更像是一个基本的 python 问题,而不是 Wxpython 本身,我不知道它希望我如何执行事件调用

我还想最终将传递的文本更改为绿色或某物或事件,但是如果我尝试对元素在 basicGUI 之外做任何事情,我无法正确引用它们(例如面板等(,因此任何关于如何实现对 GUI 元素的更改的想法都会在按钮事件之上有所帮助。

我尝试使用 button 事件复制其他人的示例,但它在我的实现中不起作用,因为我定义类的方式我认为......我需要弄清楚如何调整该行代码,以便它正确引用对象。

import wx
import ctypes
try:
ctypes.windll.shcore.SetProcessDpiAwareness(True)
except:
pass

class windowClass(wx.Frame):
def __init__(self,*args,**kwargs):
super(windowClass,self).__init__(*args,**kwargs)
self.basicGUI()
#GUI elements    
def basicGUI(self):
panel=wx.Panel(self)
menu_bar=wx.MenuBar()
box_sizer=wx.BoxSizer()
box_sizer.Add(panel, 1, wx.ALL | wx.EXPAND) 
button_text=wx.StaticText(panel, label="Select a file")
file_button=wx.FilePickerCtrl(panel)
check_button=wx.Button(panel, label='Check')
#self.Bind(wx.EVT_BUTTON, OnCheckButton(self), check_button)
a_text=wx.StaticText(panel, label="a file status")
b_text=wx.StaticText(panel, label="b file status")
c_text=wx.StaticText(panel, label="c file status")
passed_text=wx.StaticText(panel, label="passed")
#set items on the grid
sizer = wx.GridBagSizer(5, 5)
sizer.Add(button_text, (0, 0))
sizer.Add(file_button, (0, 2))
sizer.Add(check_button,(1, 2))
sizer.Add(a_text,    (2, 0))
sizer.Add(b_text,    (3, 0))
sizer.Add(c_text,    (4, 0))
sizer.Add(passed_text, (2, 1))
#make border
border = wx.BoxSizer()
border.Add(sizer, 1, wx.ALL | wx.EXPAND, 5)
#use sizers
panel.SetSizerAndFit(border)  
self.SetSizerAndFit(box_sizer)  
#show GUI
self.SetTitle('file check')
self.Centre()
self.Show(True)
def OnCheckButton(self,event):
print("perform check")   #debug line
#file_button.GetPath(self)  this probably won't work either as is, 
#don't know how to pass the button info
app=wx.App()
windowClass(None)
print("passed")
app.MainLoop()

在这个早期阶段,我希望点击按钮并能够打印或做其他事情......但是由于它说它没有定义,所以这是一个奇怪的错误。

self.Bind(wx.EVT_BUTTON, self.OnCheckButton(self), check_button)
AttributeError: 'windowClass' object has no attribute 'OnCheckButton'

首先:您有错误的缩进,OnCheckButton不是类中的方法windowClass而是basicGUI中的正常函数,因此它找不到类windowClass中的方法OnCheckButton,并且出现错误'windowClass' object has no attribute 'OnCheckButton'

第二:可能在所有GUI框架(可能在所有语言中(中,Button需要"回调" - 它意味着没有()和参数的函数名称。当您单击按钮时,系统将通过添加()来运行此功能

self.Bind(wx.EVT_BUTTON, self.OnCheckButton, check_button)

第三:在self.file_button中使用self.可以访问 metehodOnCheckButton中的此变量并获取所选文件的路径。


完整代码:

import wx
import ctypes
try:
ctypes.windll.shcore.SetProcessDpiAwareness(True)
except:
pass

class WindowClass(wx.Frame):
def __init__(self, *args, **kwargs):
super(WindowClass,self).__init__(*args, **kwargs)
self.basicGUI()
#GUI elements    
def basicGUI(self):
panel = wx.Panel(self)
menu_bar = wx.MenuBar()
box_sizer = wx.BoxSizer()
box_sizer.Add(panel, 1, wx.ALL|wx.EXPAND) 
button_text = wx.StaticText(panel, label="Select a .cpf file")
self.file_button = wx.FilePickerCtrl(panel)
check_button = wx.Button(panel, label='Check')
self.Bind(wx.EVT_BUTTON, self.OnCheckButton, check_button)
a_text = wx.StaticText(panel, label="a file status")
b_text = wx.StaticText(panel, label="b file status")
c_text = wx.StaticText(panel, label="c file status")
passed_text = wx.StaticText(panel, label="passed")
#set items on the grid
sizer = wx.GridBagSizer(5, 5)
sizer.Add(button_text, (0, 0))
sizer.Add(self.file_button, (0, 2))
sizer.Add(check_button,(1, 2))
sizer.Add(a_text, (2, 0))
sizer.Add(b_text, (3, 0))
sizer.Add(c_text, (4, 0))
sizer.Add(passed_text, (2, 1))
#make border
border = wx.BoxSizer()
border.Add(sizer, 1, wx.ALL|wx.EXPAND, 5)
#use sizers
panel.SetSizerAndFit(border)  
self.SetSizerAndFit(box_sizer)  
#show GUI
self.SetTitle('file check')
self.Centre()
self.Show(True)
# indentations are very important in Python
def OnCheckButton(self, event): 
print("perform check")   #debug line
print(self.file_button.GetPath())
app = wx.App()
WindowClass(None)
print("passed")
app.MainLoop()

顺便说一句:阅读PEP 8 - Python Code的风格指南。它建议如何在Python中格式化代码,许多人和工具都尊重这些规则。

最新更新