Python3中tkinter模块各部件的应用
import tkinter as tk
class ticket_window(tk.Frame):
#调用时初始化
def __init__(self):
global root
root = tk.Tk()
root.title('景点购票')
root.geometry('500x340+200+200')
root.resizable(0,0)
super().__init__()
#票价变量
self.ticketval = tk.StringVar()
#张数变量
self.ticketamount = tk.StringVar()
self.dic={160:'东方明珠',130:'野生动物园',60:'科技馆'}
self.pack()
self.main_window()
root.mainloop()
# 窗口布局
def main_window(self):
global root
tk.Label(root,text='请选择景点').pack()
for val in self.dic:
tk.Radiobutton(root,text=self.dic.get(val),variable=self.ticketval,value=val,command=self.ticketver).pack()
self.ticketval.set(0)
tk.Label(root,text='请输入购票张数').pack()
tk.Entry(root,textvariable=self.ticketamount).pack()
tk.Button(root,text='计算',command=self.compute).pack()
self.textout=tk.Text(root,width=50,height=10)
self.textout.pack()
def ticketver(self):
self.ticketval.get()
def compute(self):
amount=int(self.ticketamount.get())
value=int(self.ticketval.get())
if amount>=50:
total=0.8*value*amount
elif amount>=20:
total=0.95*value*amount
else:
total=float(value*amount)
outString='购'+self.dic.get(value)+str(amount)+'张,票价:'+str(total)+'元\n'
self.textout.insert('end',outString)
if __name__ == '__main__':
ticket_window()
运行效果图