Tkinter中Label,RadioButton,Button,Entry,Text部件的使用

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

在这里插入图片描述
运行效果图

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值