[问题] 请问有人可以帮我看一下Tkinter吗???谢谢!!

楼主: aeronan (aeron)   2014-05-02 13:17:33
我这写出来,不管按哪一个button只会显示橘色,有办法让它显示相对应的颜色吗??
from Tkinter import *
the_window = Tk()
the_window.title('ONE Button Colour')
start_color = 'grey'
label_bg = ['red', 'Green', 'Blue', 'Yellow', 'Orange']
button_color = ['Red', 'Green', 'Blue', 'Yellow', 'Orange']
label_colour = StringVar()
def change_colour():
if label_colour.get() == 'r':
colour['bg'] = 'red'
elif label_colour.get() == 'g':
colour['bg'] = 'green'
elif label_colour.get() == 'b':
colour['bg'] = 'blue'
elif label_colour.get() == 'y':
colour['bg'] = 'yellow'
else:
colour['bg'] = 'orange'
for r in range(5):
#for c in range(2):
colour = Label(the_window, bg = start_color, compound = 'bottom',
width = 8,height = 1)
colour.grid(row=r,column=0, padx = 2, pady = 3)
Button(the_window, text = button_color[r], command = change_colour,
width = 8,height = 1 ).grid(row=r,column=1, padx = 2, pady = 7)
the_window.mainloop()
作者: Pomay (右脚)   2014-05-02 13:42:00
判断式label_colour.get()的值没有被改变,所以都跑else不太懂你指的"对应"是怎么对应,row1 btn按完row1 label变色?还是row1 button按完 row0~4 label全变色?
楼主: aeronan (aeron)   2014-05-02 14:00:00
就是按red button,然后就会显示一个红色,我现在就是随便橘色,不知道应该要怎么改会比较好???
作者: Microscft (小软哥)   2014-05-02 14:07:00
你那一串label_bg是干嘛用的只有跑橘色 就是你的if else判断式有问题看一下StringVar的get()的说明文件
楼主: aeronan (aeron)   2014-05-02 14:25:00
我把那一串label_bg删了,因为还在初学摸索阶段,所以我找了好久还是找不到问题在哪...
作者: LiloHuang (十年一刻)   2014-05-02 16:33:00
坦白说不太懂 colour = Label(...) 每次都覆蓋的目的@@这样不管哪颗按钮按下去,应该只会更新最后一个 label
楼主: aeronan (aeron)   2014-05-02 19:05:00
其实我现在就是不知道要怎么写可以叫出那个value...有哪位大大可以帮忙吗???超级感谢!!!
作者: s860134 (s860134)   2014-05-02 19:30:00
关键在于 程式码中的colour 只是一个pointer指到Label obj所以在for 循环跑完 他只会指到最后一个Label obj
楼主: aeronan (aeron)   2014-05-02 19:46:00
所以我不能用循环?要改成把button分开打?
作者: LiloHuang (十年一刻)   2014-05-02 20:08:00
我想我上面有提到,每次都覆蓋这件事情跟你要的有冲突你可以考虑用一个 list 把生成出来的 label obj 记住假设有 labels = [] 然后 labels.append(colour) 之类的或者弄一个 dict 也是可以, key 是颜色的字串之类的
楼主: aeronan (aeron)   2014-05-02 20:55:00
谢谢各位大大的帮忙~~:)

Links booklink

Contact Us: admin [ a t ] ucptt.com