[英]How to make the definition of Combo box Classes in Tkinter to create a simple calculation
我在尝试使用Classes在tkinter中使用组合框创建简单计算时遇到问题。 对我来说,这很棘手,而且很难理解! 希望你能帮我这个忙。
提前致谢。
赫克托。
这是我的代码:
from Tkinter import *
import ttk
from ttk import Combobox
root= Tk()
root.minsize(550,450)
root.maxsize(560,460)
root.title('myAPP')
class Calculation:
def __init__(self, parent):
self.parent = parent
self.Value1()
self.Value2()
self.Result()
Label(self.parent,text='Num 1').grid(column=2, row=5,sticky=W,pady=3)
Label(self.parent,text='Num 2').grid(column=2, row=6,sticky=W,pady=3)
Label(self.parent,text='result').grid(column=9,row=9,sticky=W,pady=3)
self.msg =Label(self.parent,text='Sum of 2 number')
self.msg.grid(row=3,column=1,columnspan=2)
self.Button =Button(text='Calculate',width=8,command =self.Result)
self.Button.grid(row=9,column=2,padx=2,pady=3)
def Value1(self):
self.field1 = StringVar()
self.field1 = ttk.Combobox(self.parent, textvariable= self.field1)
self.field1['values'] = ('5', '6', '7')
self.field1.grid(column=3, row=5)
def Value2(self):
self.field2 = StringVar()
self.field2 = ttk.Combobox(self.parent, textvariable=self.field2)
self.field2['values'] = ('1', '2', '3')
self.field2.grid(column=3, row=6)
def Result(self):
self.entry = StringVar()
self.entry = ttk.Entry(self.parent, textvariable = self.entry)
#self.entry = field1 + field2 ----> Here is the problem I have!
self.entry.grid(column=3, row=9)
#End Code
if __name__ == '__main__':
app = Calculation (root)
root.mainloop()
from Tkinter import *
import ttk
root= Tk()
root.minsize(550,450)
root.maxsize(560,460)
root.title('myAPP')
class Calculation:
def __init__(self, parent):
self.parent = parent
self.Value1()
self.Value2()
self.Result()
Label(self.parent,text='Num 1').grid(column=2, row=5, sticky=W, pady=3)
Label(self.parent,text='Num 2').grid(column=2, row=6, sticky=W, pady=3)
Label(self.parent,text='result').grid(column=9,row=9, sticky=W, pady=3)
self.msg = Label(self.parent,text='Sum of 2 number')
self.msg.grid(row=3,column=1,columnspan=2)
def Value1(self):
self.field1_value = StringVar()
self.field1_value.trace('w', self.Calc)
self.field1 = ttk.Combobox(self.parent, textvariable=self.field1_value)
self.field1['values'] = ('5', '6', '7')
self.field1.grid(column=3, row=5)
def Value2(self):
self.field2_value = StringVar()
self.field2_value.trace('w', self.Calc)
self.field2 = ttk.Combobox(self.parent, textvariable=self.field2_value)
self.field2['values'] = ('1', '2', '3')
self.field2.grid(column=3, row=6)
def Result(self):
self.entry = StringVar()
self.entry = ttk.Entry(self.parent, textvariable=self.entry)
self.entry.grid(column=3, row=9)
def Calc(self, *args):
self.entry.delete(0, END)
try:
value = int(self.field1.get()) + int(self.field2.get())
except ValueError:
self.entry.insert(0, 'Input numbers.')
else:
self.entry.insert(0, str(value))
if __name__ == '__main__':
app = Calculation(root)
root.mainloop()
我将Result方法分为两种方法:Result,Calc
编辑 -删除按钮。 -将textvariable(field1_value,field2_value)附加到组合框。 -当变量更改(= comboxbox值更改)时,使用StringVar.trace调用Calc()
我认为这可以完成您要完成的任务:
from Tkinter import *
import ttk
from ttk import Combobox
class Calculation(object):
def __init__(self, parent):
self.parent = parent
self.create_widgets()
def create_widgets(self):
self.msg = Label(self.parent, text='Sum of 2 numbers:')
self.msg.grid(row=3, column=1, columnspan=2)
Label(self.parent, text='Num 1').grid(row=5, column=2, sticky=W, pady=3)
self.number1 = IntVar()
self.widget1 = ttk.Combobox(self.parent, textvariable=self.number1)
self.widget1['values'] = '5', '6', '7'
self.widget1.grid(row=5, column=3)
Label(self.parent, text='Num 2').grid(row=6, column=2, sticky=W, pady=3)
self.number2 = IntVar()
self.widget2 = ttk.Combobox(self.parent, textvariable=self.number2)
self.widget2['values'] = '1', '2', '3'
self.widget2.grid(row=6, column=3)
self.button = Button(text='Calculate', command=self.calculate_result)
self.button.grid(row=9, column=2, padx=2, pady=3)
self.result = StringVar()
self.widget3 = ttk.Entry(self.parent, textvariable=self.result)
self.widget3.grid(row=9, column=3)
Label(self.parent, text='result').grid(row=9, column=9, sticky=W, pady=3)
def calculate_result(self):
self.result.set(self.number1.get() + self.number2.get())
if __name__ == '__main__':
root= Tk()
root.minsize(550, 450)
root.maxsize(560, 460)
root.title('myAPP')
app = Calculation(root)
root.mainloop()
我也强烈建议您阅读并开始遵循PEP 8-Python代码样式指南中的建议,尤其是在代码中实体的命名方面。 另外,我认为,如果您在关键字参数的顺序和它们之间的间距上更一致,那么这将对您的编码样式有很大的改善。 最好早点建立正确的做事方式,这样它们就会成为您甚至以后不必考虑做事的习惯。
您的接口代码将分为两部分:在初始化时将运行对小部件的描述以及应用程序的动态行为。
您已经有了GUI的描述。 对于此行为,在按钮上定义command
是一个很好的开始,但是此代码与您的Result
方法(用于创建条目)的外观不同。 在此方法(称为回调)中,您将操作已经存在的对象:读取comboxboxes的值,设置条目的新值...例如(改编自falsetru的答案 ) :
self.button = Button(... , command=self.calcultate)
def calculate(self):
self.entry.delete(0, END)
value = int(self.field1.get()) + int(self.field2.get())
self.entry.insert(0, str(value))
还有其他定义应用程序行为的方法,例如,您可以将变量与具有值(条目,组合框...)的小部件相关联,并在修改此变量后立即触发操作:
self.number1 = IntVar()
self.field1 = ttk.Combobox(self.parent, textvariable=self.number1)
self.number1.trace("w", self.calculate)
(请注意,我选择使用IntVar以避免对先前解决方案进行int()
解析的需要)。
为避免用户在组合框中输入非数字时出现错误,可以在读取值时捕获异常,也可以使用验证命令禁止非数字输入。
def ensure_digit(candidate_string):
return candidate_string.isdigit()
self.field1.config(validate = "key", validatecommand =(self.field1.register(ensure_digit), "%S"))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.