[英]WxPython GUI programming - NameError: name “self” is not defined
[英]WxPython GUI programming.
所以我的问题是,我需要在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()
参见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()
我真的可以直接将事件绑定到它上……但是我想保留大部分代码
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.