0

所以我的问题是,我需要在 wxpython 中创建一个 GUI 来计算汉堡包的价格。每个额外的成分是1.55,小价是1.55,中价是1.55加1.55等等。我的问题是:我如何分配单选按钮,就像我想这样做一样:如果选择了 radiobutton1:这样做

与复选框的数量相同。继承人我的脚本到目前为止。

import wx

class Window(wx.Frame):
    def __init__(self, parent, title):
        wx.Frame.__init__(self, parent, title=title, size=(400, 250))
        self.title = title
        self.initGUI()

    def initGUI(self):
        # Set up the widgets for the GUI
        panel = wx.Panel(self, -1)
        self.amount = wx.StaticText(panel, -1, 'Ingredients = 0', pos=(10, 10))

        #                      Parent ID  Value         Position (Height, Width)
        self.cb1 = wx.CheckBox(panel, -1, 'Extra Patty', (10, 30))
        self.cb2 = wx.CheckBox(panel, -1, 'Cheese', (10, 50))
        self.cb3 = wx.CheckBox(panel, -1, 'Onions', (10, 70))
        self.cb4 = wx.CheckBox(panel, -1, 'Mushrooms', (10,90))
        # Register an event for each checkbox.
        self.Bind(wx.EVT_CHECKBOX, self.toggleIngredients, self.cb1)
        self.Bind(wx.EVT_CHECKBOX, self.toggleIngredients, self.cb2)
        self.Bind(wx.EVT_CHECKBOX, self.toggleIngredients, self.cb3)
        self.Bind(wx.EVT_CHECKBOX, self.toggleIngredients, self.cb4)
    self.rb1 = wx.RadioButton(panel, -1, 'Small', (200,30))
    self.rb2 = wx.RadioButton(panel, -1, 'Medium', (200,50))
    self.rb3 = wx.RadioButton(panel, -1, 'Large', (200, 70))

        self.Show()
        self.Centre()

    def toggleIngredients(self, event):
        self.ingredients = 0
        for cb in (self.cb1, self.cb2, self.cb3, self.cb4):
            if cb.IsChecked():
                self.ingredients += 1
        self.amount.SetLabel('Ingredients = ' + str(self.ingredients))

    if self.amount.SetLabel == '1':
        ing = 1
    if self.amount.SetLabel == '2':
        ing = 2
    if self.amount.SetLabel == '3':
        ing = 3
    if self.amount.SetLabel == '4':
        ing = 4


app = wx.App(False)
window = Window(None, 'CheckBox Example')
app.MainLoop()
4

1 回答 1

0

请参阅 UpdateCost 方法

    import wx

class Window(wx.Frame):
    def __init__(self, parent, title):
        wx.Frame.__init__(self, parent, title=title, size=(400, 250))
        self.title = title
        self.initGUI()

    def initGUI(self):
        # Set up the widgets for the GUI
        panel = wx.Panel(self, -1)
        self.amount = wx.StaticText(panel, -1, 'Ingredients = 0', pos=(10, 10))

        #                      Parent ID  Value         Position (Height, Width)
        self.cbs = [
                    wx.CheckBox(panel, -1, 'Extra Patty', (10, 30)),
                    wx.CheckBox(panel, -1, 'Cheese', (10, 50)),
                    wx.CheckBox(panel, -1, 'Onions', (10, 70)),
                    wx.CheckBox(panel, -1, 'Mushrooms', (10,90))
                    ]
        # Register an event for each checkbox.
        self.Bind(wx.EVT_CHECKBOX, self.toggleIngredients)
        self.radios = [
                    wx.RadioButton(panel, -1, 'Small', (200,30)),
                    wx.RadioButton(panel, -1, 'Medium', (200,50)),
                    wx.RadioButton(panel, -1, 'Large', (200, 70))
                    ]
        self.Bind(wx.EVT_RADIOBUTTON,self.toggleIngredients)
        self.Show()
        self.Centre()
    def UpdateCost(self):
        costs = {"Small":1.55,"Medium":3.10,"Large":4.65}
        base_cost = 0.00
        for r in self.radios:
            if r.GetValue():
                base_cost = costs[r.GetLabel()]
        additional_costs = sum([1.5 for x in self.cbs if x.GetValue()])
        self.amount.SetLabel("$%0.2f"%(base_cost+additional_costs))
    def toggleIngredients(self, event):
        self.UpdateCost()


app = wx.App(False)
window = Window(None, 'CheckBox Example')
app.MainLoop()

我真的可以直接将事件绑定到它......但我想保留你的大部分代码

于 2012-07-31T17:16:15.943 回答