import tkinter as tk from tkinter import ttk import tkinter.messagebox import demo def del_select(): for item in tree_view.get_children(''): tree_view.delete(item) def GetTicket(): start_city = from_station.get() end_city = to_station.get() date = from_date.get() if len(start_city) >= 1 and len(end_city) >= 1 and len(date) >= 1: tickets = demo.Ticket(start_city=start_city, end_city=end_city, date=date) # tickets = [ # {'SerialNo': 0, 'TravelNumber': 'G539', 'DepartureTime': '06:52', 'ArrivalTime': '08:11', 'TimeTime': '01:19', 'PremiumSeat': '无', 'FirstClassSeat': '无', 'SecondClassSeat': '有', 'SoftSleeper': '', 'HardSleeper': '', 'HardSeat': '', 'Noseat': ''}, # ] # 往树状图中插入数据 del_select() for index, ticket in enumerate(tickets): tree_view.insert('', index + 1, values=( ticket['SerialNo'], ticket['TravelNumber'], ticket['DepartureTime'], ticket['ArrivalTime'], ticket['TimeTime'], ticket['PremiumSeat'], ticket['FirstClassSeat'], ticket['SecondClassSeat'], ticket['SoftSleeper'], ticket['HardSleeper'], ticket['HardSeat'], ticket['Noseat'], )) else: tkinter.messagebox.showerror(title='警告', message='输入有误') root = tk.Tk() root.title('火车票查票小软件') root.iconbitmap("img\\search.ico") root.geometry('1040x620+500+500') img = tk.PhotoImage(file='img\封面png.png') tk.Label(root, image=img).pack() # 查询界面 Top_frame = tk.LabelFrame(root) Top_frame.pack(pady=10) left_frame = tk.Frame(Top_frame) left_frame.pack(side=tk.LEFT) v = tk.IntVar() v.set(1) tk.Radiobutton(left_frame, text='单程', font=('微软雅黑', 12), variable=v, value=1).pack() tk.Radiobutton(left_frame, text='往返', font=('微软雅黑', 12), variable=v, value=2).pack() # ------------------------------------------------------------ mid_frame = tk.Frame(Top_frame) mid_frame.pack(side=tk.LEFT, padx=10) from_station = tk.StringVar() tk.Label(mid_frame, text='出发地', font=('微软雅黑', 12)).pack(padx=10, side=tk.LEFT) tk.Entry(mid_frame, width=10, textvariable=from_station).pack(side=tk.LEFT) tk.Label(mid_frame, text='↔', font=('微软雅黑', 12), fg='#0096c7').pack(padx=20, side=tk.LEFT) to_station = tk.StringVar() tk.Label(mid_frame, text='目的地', font=('微软雅黑', 12)).pack(side=tk.LEFT) tk.Entry(mid_frame, width=10, textvariable=to_station).pack(padx=10, side=tk.LEFT) from_date = tk.StringVar() from_date.set('2024-06-01') tk.Label(mid_frame, text='出发日', font=('微软雅黑', 12)).pack(padx=20, side=tk.LEFT) tk.Entry(mid_frame, width=10, textvariable=from_date).pack(side=tk.LEFT) to_date = tk.StringVar() tk.Label(mid_frame, text='车次序号', font=('微软雅黑', 12)).pack(padx=20, side=tk.LEFT) tk.Entry(mid_frame, width=10, textvariable=to_date).pack(side=tk.LEFT) # ------------------------------------------------------------ right_frame = tk.Frame(Top_frame) right_frame.pack(side=tk.LEFT, padx=10) v_1 = tk.IntVar() v_1.set(1) tk.Radiobutton(right_frame, text='普通', font=('微软雅黑', 12), variable=v_1, value=1).pack(padx=10) tk.Radiobutton(right_frame, text='学生', font=('微软雅黑', 12), variable=v_1, value=2).pack(padx=10) Button_frame = tk.Frame(Top_frame) Button_frame.pack(side=tk.LEFT, padx=10) # tk.Button(Button_frame, text='购票', font=('微软雅黑', 12), width=10, bg='#f79209', fg='white', relief='flat', command=Buy).pack(pady=5) tk.Button(Button_frame, text='查询', font=('微软雅黑', 12), width=10, bg='#f79209', fg='white', relief='flat', command=GetTicket).pack(pady=5) # ------------------------------------------------------------ columns = ("SerialNo", "TravelNumber", "DepartureTime", "ArrivalTime", "TimeTime", "PremiumSeat", "FirstClassSeat", "SecondClassSeat", 'SoftSleeper', "HardSleeper", "HardSeat", "Noseat") columns_value = ("序号", "车次", "出发时间", "到达时间", "耗时", "特等座", "一等座", "二等座", '软卧', "硬卧", "硬座", "无座") # 使用ttk创建一个树状图 tree_view = ttk.Treeview(root, height=18, show="headings", columns=columns) # 设置列名 tree_view.column('SerialNo', width=80, anchor='center') tree_view.column('TravelNumber', width=80, anchor='center') tree_view.column('DepartureTime', width=80, anchor='center') tree_view.column('ArrivalTime', width=80, anchor='center') tree_view.column('TimeTime', width=80, anchor='center') tree_view.column('PremiumSeat', width=80, anchor='center') tree_view.column('FirstClassSeat', width=80, anchor='center') tree_view.column('SecondClassSeat', width=80, anchor='center') tree_view.column('SoftSleeper', width=80, anchor='center') tree_view.column('HardSleeper', width=80, anchor='center') tree_view.column('HardSeat', width=80, anchor='center') tree_view.column('Noseat', width=80, anchor='center') # 给列名设置显示的名字 tree_view.heading('SerialNo', text='序号') tree_view.heading('TravelNumber', text='车次') tree_view.heading('DepartureTime', text='出发时间') tree_view.heading('ArrivalTime', text='到达时间') tree_view.heading('TimeTime', text='耗时') tree_view.heading('PremiumSeat', text='特等座') tree_view.heading('FirstClassSeat', text='一等座') tree_view.heading('SecondClassSeat', text='二等座') tree_view.heading('SoftSleeper', text='软卧') tree_view.heading('HardSleeper', text='硬卧') tree_view.heading('HardSeat', text='硬座') tree_view.heading('Noseat', text='无座') tree_view.pack(side=tk.LEFT, fill=tk.BOTH, expand=True) # 创建滚动窗口并布局到页面上 sb = tk.Scrollbar(root) sb.pack(side=tk.RIGHT, fill=tk.Y) sb.config(command=tree_view.yview) root.mainloop()