WxPython GUI编程。

时间:2012-07-31 16:49:35

标签: python wxpython

所以我的问题是,我需要在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()

1 个答案:

答案 0 :(得分: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()

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