GUI程序单项选择颜色后字体的颜色可以变,但运行后不会报错,程序也不会显示是什么原因,求大佬帮忙

import tkinter as tk

mywindow = tk.Tk()
mywindow.geometry(‘260x100’)
mywindow.title(‘单选按钮控件’)
mylab = tk.Label(mywindow,
text=’利用单选按钮控件修改字体颜色’,
font=(‘黑体’, 12),
width=150,
height=3
)
mylab.pack()

color = tk.StringVar()
myrb1 = tk.Radiobutton(mywindow, text=’红色’, variable=color, value=’red’)
myrb1.pack(side=tk.LEFT)
myrb2 = tk.Radiobutton(mywindow, text=’绿色’, variable=color, value=’green’)
myrb2.pack(side=tk.LEFT)
myrb3 = tk.Radiobutton(mywindow, text=’蓝色’, variable=color, value=’blue’)
myrb3.pack(side=tk.LEFT)
myrb4 = tk.Radiobutton(mywindow, text=’紫色’, variable=color, value=’purple’)
myrb4.pack(side=tk.LEFT)
myrb5 = tk.Radiobutton(mywindow, text=’粉色’, variable=color, value=’pink’)
myrb5.pack(side=tk.LEFT)

Jason990420
最佳答案
import tkinter as tk

def radioclick():
    mylab.config(fg=color.get())

mywindow = tk.Tk()
mywindow.geometry("260x100")
mywindow.title ("单选按钮控件")
mylab = tk.Label(mywindow, text="利用单选按钮控件修改字体颜色", font=("黑体", 12), width=150, height=3, fg="red")
mylab.pack()

color = tk.StringVar()
myrb1 = tk.Radiobutton (mywindow, text="红色", variable=color, value="red", command=radioclick)
myrb1.pack(side=tk.LEFT)
myrb2 = tk.Radiobutton (mywindow, text="绿色", variable=color, value="green", command=radioclick)
myrb2.pack(side=tk.LEFT)
myrb3 = tk.Radiobutton (mywindow, text="蓝色", variable=color, value="blue", command=radioclick)
myrb3.pack(side=tk.LEFT)
myrb4 = tk.Radiobutton (mywindow, text="紫色", variable=color, value="purple", command=radioclick)
myrb4.pack(side=tk.LEFT)
myrb5 = tk.Radiobutton (mywindow, text="粉色", variable=color, value="pink", command=radioclick)
myrb5.pack(side=tk.LEFT)

color.set("red")

mywindow.mainloop()
1个月前 评论
讨论数量: 2
Jason990420

你的代码不完整, 因此问题的标题看起来就不对, 提供以下范例代码供参考.

file

import tkinter as tk

def label_update(i):
    mylab.configure(fg=colors[i])

default = 2
color_text = ["红色", "绿色", "蓝色", "紫色", "粉色"]
colors = ['red', 'green', 'blue', 'purple', 'pink']

mywindow = tk.Tk()
mywindow.geometry("260x100")
mywindow.title("单选按钮控件")

mylab = tk.Label(
    mywindow, text="利用单选按钮控件修改字体颜色", font=("Courier New", 12),
    width=150, height=3, fg=colors[default])
mylab.pack()

select = tk.IntVar()
myrb = [None]*len(colors)
for value, (text, color) in enumerate(zip(color_text, colors)):
    myrb[value] = tk.Radiobutton(
        mywindow, text=text, variable=select, value=value, fg=color,
        command=lambda index=value: label_update(index))
    myrb[value].pack(side=tk.LEFT)
select.set(default)

mywindow.mainloop()
1个月前 评论
honour (楼主) 1个月前
Jason990420
import tkinter as tk

def radioclick():
    mylab.config(fg=color.get())

mywindow = tk.Tk()
mywindow.geometry("260x100")
mywindow.title ("单选按钮控件")
mylab = tk.Label(mywindow, text="利用单选按钮控件修改字体颜色", font=("黑体", 12), width=150, height=3, fg="red")
mylab.pack()

color = tk.StringVar()
myrb1 = tk.Radiobutton (mywindow, text="红色", variable=color, value="red", command=radioclick)
myrb1.pack(side=tk.LEFT)
myrb2 = tk.Radiobutton (mywindow, text="绿色", variable=color, value="green", command=radioclick)
myrb2.pack(side=tk.LEFT)
myrb3 = tk.Radiobutton (mywindow, text="蓝色", variable=color, value="blue", command=radioclick)
myrb3.pack(side=tk.LEFT)
myrb4 = tk.Radiobutton (mywindow, text="紫色", variable=color, value="purple", command=radioclick)
myrb4.pack(side=tk.LEFT)
myrb5 = tk.Radiobutton (mywindow, text="粉色", variable=color, value="pink", command=radioclick)
myrb5.pack(side=tk.LEFT)

color.set("red")

mywindow.mainloop()
1个月前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!