import tkinter as tk
class yemian1:
def showone(self):
root1 = tk.Tk()
root1.geometry('300x400')
self.label = tk.Label(root1)
self.one = tk.StringVar(root1)
one1 = tk.Entry(root1, width=80, textvariable=self.one)
button1 = tk.Button(root1, text="1,求一个数组所有数的和", command=self.cal1) # 将按钮绑定到cal1方法
button2 = tk.Button(root1, text=",2,", command=self.cal2)
button3 = tk.Button(root1, text=",3,", command=self.cal3)
button4 = tk.Button(root1, text=",4,", command=self.cal4)
button5 = tk.Button(root1, text=",5,", command=self.cal5)
button6= tk.Button(root1, text="6,", command=self.cal6)
one1.pack()
button1.pack()
button2.pack()
button3.pack()
button4.pack()
button5.pack()
button6.pack()
self.label.pack()
root1.mainloop() # 显示root1窗口
def cal1(self):
self.obj = getone(self.one.get(),self.label)
self.obj.fun1()
def cal2(self):
self.obj = getone(self.one.get(),self.label)
self.obj.fun2()
def cal3(self):
self.obj = getone(self.one.get(),self.label)
self.obj.fun3()
def cal4(self):
self.obj = getone(self.one.get(),self.label)
self.obj.fun4()
def cal5(self):
self.obj = getone(self.one.get(),self.label)
self.obj.fun5()
def cal6(self):
self.obj = getone(self.one.get(),self.label)
self.obj.fun6()
class getone:
def __init__(self, a,label):
self.a = a
self.label=label
def fun1(self):
l=self.a.split()
sum=0
for i in l:
sum+=int(i)
self.label.config(text=str(sum))
root = tk.Tk()
root.title('python_exercise')
root['height'] = 300
root['width'] = 400
but1 = tk.Button(root, command=yemian1().showone, text='填一个')
but1.pack()
root.mainloop()
类和tkinter的简单结合
最新推荐文章于 2024-04-13 09:10:24 发布