2012-07-31 101 views
0

所以我的問題是,我需要在wxpython中製作一個GUI來計算漢堡的價格。每個額外的成分是1.55,小的價格是1.55,中等的是1.55加1.55,依此類推。我的問題是:我該如何分配單選按鈕,就像如果我想這樣做: 如果選擇radiobutton1: 做到這一點WxPython GUI編程。

,並用相同的檢查箱子的數量。繼承我的劇本到目前爲止。

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() 
+0

我也是不知道怎麼的做一個文本框類型的東西,總成本出現。即時通訊在wxpython的noob – 2012-07-31 16:50:09

+1

您是否試過[WxPython教程](http://www.google.com/search?q=wxpython+tutorial)? – 2012-07-31 16:55:30

回答

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

我真的可以只綁定的事件,直接...但我想離開大部分代碼不變