通过向定义传递参数来缩短GUI代码

2024-09-30 12:21:10 发布

您现在位置:Python中文网/ 问答频道 /正文

我在wxpython中编写了一个GUI代码,它或多或少地重复了4次相同的信息。屏幕上有很多按钮,我不得不将事件绑定到这些按钮上,我发现我有很多看起来几乎完全相同的on\u button\u click定义。所以,我想知道,在将按钮绑定到事件并删除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\u clicked更改为VDBenchslotx\u clicked(),并将参数传递给它,但有两种情况发生:它告诉我输入的参数与def的参数不匹配,或者它让我的程序运行,但它会在程序启动时自动执行def,而不是在按下按钮时,然后按下按钮不能正常工作。你知道吗


Tags: thenoselfdefbutton按钮wxdisk
2条回答

使用lambda表达式将参数传递给绑定函数。例如:

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])

dbc(和the linked question)展示了如何实现您想要的。但下面是我在上面的评论中提到的helper函数概念的一个简短演示。你知道吗

def make_button(text, callback):
    button = wx.Button(self, -1, text)
    sizer.Add(button, (1, 5), (1, 5), wx.EXPAND)
    button.Bind(wx.EVT_BUTTON, callback)
    return button

self.VDBenchSlot1 = make_button("Slot 1 VDBench", self.VDBenchSlot1_clicked)
self.VDBenchSlot2 = make_button("Slot 2 VDBench", self.VDBenchSlot2_clicked)

注意,make_button的签名中没有self。这是因为它不是一个方法,而是一个在方法内部定义的函数。有关GTK2+中完全运行的示例,请参见this answer。你知道吗

还要注意,我的代码是基于您的原始代码的,但是您应该可以很容易地对其进行调整,以使用dbc新的VDBenchSlot_clicked回调方法。你知道吗

相关问题 更多 >

    热门问题