1 回答

TA贡献1841条经验 获得超3个赞
第一:你有错误的缩进,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 中格式化代码,许多人和工具都遵守这些规则。
添加回答
举报