我一直在 wxpython 中编写 GUI 代码,它或多或少地重复相同的信息 4 次。屏幕上有很多按钮,我必须将事件绑定(bind)到,并且我发现我有很多看起来几乎相同的 on_button_click 定义。所以,我想知道是否有一种方法可以在将按钮绑定(bind)到事件并删除 3 个定义时仅传递参数。这是一个例子:
self.VDBenchSlot1 = wx.Button(self, -1, "Slot 1 VDBench")
sizer.Add(self.VDBenchSlot1,(1, 5), (1, 5), wx.EXPAND)
self.VDBenchSlot1.Bind(wx.EVT_BUTTON, self.VDBenchSlot1_clicked)
self.VDBenchSlot2 = wx.Button(self, -1, "Slot 2 VDBench")
sizer.Add(self.VDBenchSlot2,(1, 5), (1, 5), wx.EXPAND)
self.VDBenchSlot2.Bind(wx.EVT_BUTTON, self.VDBenchSlot2_clicked)
self.VDBenchSlot3 = wx.Button(self, -1, "Slot 3 VDBench")
sizer.Add(self.VDBenchSlot3,(1, 5), (1, 5), wx.EXPAND)
self.VDBenchSlot3.Bind(wx.EVT_BUTTON, self.VDBenchSlot3_clicked)
self.VDBenchSlot4 = wx.Button(self, -1, "Slot 4 VDBench")
sizer.Add(self.VDBenchSlot4,(1, 5), (1, 5), wx.EXPAND)
self.VDBenchSlot4.Bind(wx.EVT_BUTTON, self.VDBenchSlot4_clicked)
def VDBenchSlot1_clicked(self, event):
global diskchange
if diskchange[1] == 'No Disk':
self.TextSlot1.AppendText("No Disk is currently in the slot so you cannot run this! \n")
else:
# Open the file startDisk#VD.sh that has the setup to start running VDBench
os.system("echo pcieRocks | sudo -S gnome-terminal --profile=VDbench --working-directory=/home/pciedev3ubuntu/Documents -e './vdbench -f disk%dVDscript.txt -vr' &" %diskchange[1])
def VDBenchSlot2_clicked(self, event):
global diskchange
if diskchange[2] == 'No Disk':
self.TextSlot2.AppendText("No Disk is currently in the slot so you cannot run this! \n")
else:
# Open the file startDisk#VD.sh that has the setup to start running VDBench
os.system("echo pcieRocks | sudo -S gnome-terminal --profile=VDbench --working-directory=/home/pciedev3ubuntu/Documents -e './vdbench -f disk%dVDscript.txt -vr' &" %diskchange[2])
def VDBenchSlot3_clicked(self, event):
global diskchange
if diskchange[3] == 'No Disk':
self.TextSlot3.AppendText("No Disk is currently in the slot so you cannot run this! \n")
else:
# Open the file startDisk#VD.sh that has the setup to start running VDBench
os.system("echo pcieRocks | sudo -S gnome-terminal --profile=VDbench --working-directory=/home/pciedev3ubuntu/Documents -e './vdbench -f disk%dVDscript.txt -vr' &" %diskchange[3])
def VDBenchSlot4_clicked(self, event):
global diskchange
if diskchange[4] == 'No Disk':
self.TextSlot4.AppendText("No Disk is currently in the slot so you cannot run this! \n")
else:
# Open the file startDisk#VD.sh that has the setup to start running VDBench
os.system("echo pcieRocks | sudo -S gnome-terminal --profile=VDbench --working-directory=/home/pciedev3ubuntu/Documents -e './vdbench -f disk%dVDscript.txt -vr' &" %diskchange[4])
我尝试将 VDBenchslotx_clicked 更改为 VDBenchslotx_clicked() 并向其传递参数,但发生了以下两种情况之一:它告诉我输入的参数与 def 的参数不匹配,或者它让我的程序运行,但它在程序启动时自动执行 def,而不是在按下按钮时执行,然后按钮无法正常工作。
最佳答案
使用 lambda 表达式将参数传递给绑定(bind)函数。例如:
self.VDBenchSlot1.Bind(wx.EVT_BUTTON, lambda event: self.VDBenchSlot_clicked(event, 1))
def VDBenchSlot_clicked(self, event, position):
if position == 1:
text_slot = self.TextSlot1
elif position == 2:
text_slot = self.TextSlot2
elif position == 3:
text_slot = self.TextSlot3
elif position == 4:
text_slot = self.TextSlot4
global diskchange
if diskchange[position] == 'No Disk':
text_slot.AppendText("No Disk is currently in the slot so you cannot run this! \n")
else:
# Open the file startDisk#VD.sh that has the setup to start running VDBench
os.system("echo pcieRocks | sudo -S gnome-terminal --profile=VDbench --working-directory=/home/pciedev3ubuntu/Documents -e './vdbench -f disk%dVDscript.txt -vr' &" %diskchange[position])
关于python - 通过将参数传递给定义来缩短 GUI 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36478731/