# -*- coding: utf-8 -*- import tkinter as tk import tkinter.ttk as ttk root = tk.Tk() root.title("tk26.py") root.geometry("300x250") ##################################### def sel(): selection = "Selected " if var.get()==1: selection = selection + str(var.get()) + " " + rb1.cget('text') rb1.config(fg = 'blue') else: rb1.config(fg = 'black') if var.get()==2: selection = selection + str(var.get()) + " " + rb2.cget('text') rb2.config(fg = 'blue') else: rb2.config(fg = 'black') if var.get()==3: selection = selection + str(var.get()) + " " + rb3.cget('text') rb3.config(fg = 'blue') else: rb3.config(fg = 'black') lb.config(text = selection) def tsel(): selection = "Selected " if v.get()==1: selection = selection + str(v.get()) + " " + trb1.cget('text') ttk.Style().configure('rb1.TRadiobutton', foreground = 'blue') else: ttk.Style().configure('rb1.TRadiobutton', foreground = 'black') if v.get()==2: selection = selection + str(v.get()) + " " + trb2.cget('text') ttk.Style().configure('rb2.TRadiobutton', foreground = 'blue') else: ttk.Style().configure('rb2.TRadiobutton', foreground = 'black') if v.get()==3: selection = selection + str(v.get()) + " " + trb3.cget('text') ttk.Style().configure('rb3.TRadiobutton', foreground = 'blue') else: ttk.Style().configure('rb3.TRadiobutton', foreground = 'black') tlb.config(text = selection) lFrame = ttk.Frame(root) var = tk.IntVar() rb1 = tk.Radiobutton(lFrame, text = "大雄", variable = var, value = 1, command = sel) rb1.grid(row = 0, column = 0, sticky = tk.W) rb2 = tk.Radiobutton(lFrame, text = "靜香", variable = var, value = 2, command = sel) rb2.grid(row = 1, column = 0, sticky = tk.W) rb3 = tk.Radiobutton(lFrame, text = "哆啦A夢", variable = var, value = 3, command = sel) rb3.grid(row = 2, column = 0, sticky = tk.W) lb = tk.Label(lFrame, text = 'Label') lb.grid(row = 4, column = 0, sticky = tk.W) lFrame.pack(side = tk.LEFT) rFrame = ttk.Frame(root) v = tk.IntVar() trb1 = ttk.Radiobutton(rFrame, text = "Benz", variable = v, value = 1, command = tsel, style = 'rb1.TRadiobutton') trb1.grid(row = 0, column = 0, sticky = tk.W) trb2 = ttk.Radiobutton(rFrame, text = "BMW", variable = v, value = 2, command = tsel, style = 'rb2.TRadiobutton') trb2.grid(row = 1, column = 0, sticky = tk.W) trb3 = ttk.Radiobutton(rFrame, text = "Ferrari", variable = v, value = 3, command = tsel, style = 'rb3.TRadiobutton') trb3.grid(row = 2, column = 0, sticky = tk.W) tlb = ttk.Label(rFrame, text = 'Label') tlb.grid(row = 4, column = 0) rFrame.pack(side = tk.RIGHT) ##################################### root.mainloop()