python - 如何正确实现按钮单击事件以及如何动态更改 GUI 元素
问题描述
我做了一个丑陋的小 GUI,遵循一些教程并阅读了我对基本 WxPython 的理解,我目前正在使用 python 2.6。我设法让大多数东西显示出来,但是当我单击检查按钮时,我基本上想在文件选择器按钮上获取文件路径,但是当我尝试进行绑定时,我收到一条错误消息,指出该对象没有该属性。
在我看来,这比 Wxpython 本身更像是一个基本的 Python 问题,我不知道它希望我如何执行事件调用
此外,我想最终将传递的文本更改为绿色或其他内容或事件,但如果我尝试使用元素在 basicGUI 之外做任何事情,我无法正确引用它们(如面板等),所以关于如何的任何想法在按钮事件之上实现对 GUI 元素的更改将很有帮助。
我尝试使用按钮事件复制其他人的示例,但它在我的实现中不起作用,因为我定义类的方式我认为......我需要弄清楚如何调整那行代码以便它引用对象适当地。
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
找不到方法,你得到错误OnCheckButton
windowClass
'windowClass' object has no attribute 'OnCheckButton'
第二:可能在所有 GUI 框架(可能在所有语言中)都Button
需要“回调”——这意味着没有()
参数的函数名称。当您单击按钮时,系统将通过添加来运行此功能()
self.Bind(wx.EVT_BUTTON, self.OnCheckButton, check_button)
第三:使用self.
in 在self.file_button
方法中访问该变量OnCheckButton
并获取所选文件的路径。
完整代码:
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 代码样式指南。它建议如何在 Python 中格式化代码,许多人和工具都遵守这些规则。
推荐阅读
- mathjax - MathJax 3.0.1 排版失败时出现错误
- javascript - 引导滑块动画问题
- fortran - 错误:无法更改 OPEN 语句中的 ACTION 参数
- sparql - 多个层次关系的 SPARQL 传递查询
- angular - ag-grid-angular 不显示数据
- javascript - 如何在 React 中使用 i18next 更改语言
- python - 如何合并具有不同索引的两个不同变量?
- react-native - 如何在 React Navigation 5 中将父函数传递给子屏幕?
- javascript - 如何使我的范围滑块适用于数字和颜色?
- python - 类型错误:+ 不支持的操作数类型:statsmodels SimpleExpoentialSmoothing 的“时间戳”和“NoneType”