# -*- coding: utf-8 -*- import tkinter as tk import tkinter.ttk as ttk root = tk.Tk() root.title("tk24.py") root.geometry("300x250") ##################################### def fun1(event): tex = "" if var1.get()==1: tex = tex + (cb1.cget("text")) + " " if var2.get()==1: tex = tex + (cb2.cget("text")) + " " if var3.get()==1: tex = tex + (cb3.cget("text")) + " " if var4.get()==1: tex = tex + (cb4.cget("text")) + " " if var5.get()==1: tex = tex + (cb5.cget("text")) + " " lb1.config(text = "{} {} {} {} {}\n你選擇了{}".format (var1.get(), var2.get(), var3.get(), var4.get(), var5.get(), tex)) def fun2(): tex = "" if v1.get()==1: tex = tex + (tcb1.cget("text")) + " " if v2.get()==1: tex = tex + (tcb2.cget("text")) + " " if v3.get()==1: tex = tex + (tcb3.cget("text")) + " " if v4.get()==1: tex = tex + (tcb4.cget("text")) + " " if v5.get()==1: tex = tex + (tcb5.cget("text")) + " " lb2.config(text = "{} {} {} {} {}\n你選擇了{}".format (v1.get(), v2.get(), v3.get(), v4.get(), v5.get(), tex)) def cb1fg(): if var1.get() == 1: cb1.config(fg = "red") else: cb1.config(fg = "black") def tcb1fg(): if v1.get() == 1: ttk.Style().configure('tcb1.TCheckbutton',foreground = "red") else: ttk.Style().configure('tcb1.TCheckbutton',foreground = "black") leftF = ttk.Frame(root) # Left Frame var1 = tk.IntVar() cb1 = tk.Checkbutton(leftF, text = "大雄", variable = var1, command = cb1fg) cb1.grid(row = 0, column = 0, sticky = tk.W) var2 = tk.IntVar() cb2 = tk.Checkbutton(leftF, text = "靜香", variable = var2) cb2.grid(row = 1, column = 0, sticky = tk.W) var3 = tk.IntVar() cb3 = tk.Checkbutton(leftF, text = "胖虎", variable = var3) cb3.grid(row = 2, column = 0, sticky = tk.W) var4 = tk.IntVar() cb4 = tk.Checkbutton(leftF, text = "哆啦A夢", variable = var4) cb4.grid(row = 3, column = 0, sticky = tk.W) var5 = tk.IntVar() cb5 = tk.Checkbutton(leftF, text = "小夫", variable = var5) cb5.grid(row = 4, column = 0, sticky = tk.W) btn1 = tk.Button(leftF, text = "Choose") btn1.grid(row = 5, column = 0, sticky = tk.W) lb1 = tk.Label(leftF) lb1.grid(row = 6, column = 0, sticky = tk.W) btn1.bind("<1>", fun1) rightF = ttk.Frame(root) # Right Frame v1 = tk.IntVar() tcb1 = ttk.Checkbutton(rightF, text = "Ford", variable = v1, command = tcb1fg, style = 'tcb1.TCheckbutton') tcb1.grid(row = 0, column = 0, sticky = tk.W) v2 = tk.IntVar() tcb2 = ttk.Checkbutton(rightF, text = "Benz", variable = v2) tcb2.grid(row = 1, column = 0, sticky = tk.W) v3 = tk.IntVar() tcb3 = ttk.Checkbutton(rightF, text = "BMW", variable = v3) tcb3.grid(row = 2, column = 0, sticky = tk.W) v4 = tk.IntVar() tcb4 = ttk.Checkbutton(rightF, text = "Toyota", variable = v4) tcb4.grid(row = 3, column = 0, sticky = tk.W) v5 = tk.IntVar() tcb5 = ttk.Checkbutton(rightF, text = "Mitsubishi", variable = v5) tcb5.grid(row = 4, column = 0, sticky = tk.W) btn2 = tk.Button(rightF, text = "Choose", command = fun2) btn2.grid(row = 5, column = 0, sticky = tk.W) lb2 = tk.Label(rightF) lb2.grid(row = 6, column = 0, sticky = tk.W) leftF.pack(side = tk.LEFT) rightF.pack(side = tk.RIGHT) ##################################### root.mainloop()