首页 > 解决方案 > 如何正确实现按钮单击事件以及如何动态更改 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'

标签: pythoneventswxpython

解决方案


第一:你有错误的缩进,OnCheckButton不是类中的方法,windowClass而是里面的正常函数,所以它在类中basicGUI找不到方法,你得到错误OnCheckButtonwindowClass'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 中格式化代码,许多人和工具都遵守这些规则。


推荐阅读