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

How to properly implement event for button click and how to change GUI elements on the fly

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

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

我还想最终将传递的文本更改为绿色或其他内容或事件,但是如果我尝试使用元素在 basicGUI 之外做任何事情,我将无法正确引用它们(比如面板等)所以关于如何对 GUI 元素进行更改的任何想法都会对按钮事件有所帮助。

我尝试复制其他人使用按钮事件的示例,但它在我的实现中不起作用,因为我定义 class 的方式我认为...我需要弄清楚如何适应那行代码因此它正确地引用了对象。

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 不是 class windowClass 中的方法,而是 basicGUI 中的正常函数,所以它找不到方法 OnCheckButton 在 class windowClass 中出现错误 'windowClass' object has no attribute 'OnCheckButton'

其次:可能在所有 GUI 框架中(可能在所有语言中)Button 需要 "callback" - 这意味着没有 () 和参数的函数名称。当你点击按钮时,系统将运行这个功能通过添加()

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 -- Style Guide for Python Code。它建议如何在 Python 中格式化代码,许多人和工具都遵守这些规则。