【发布时间】:2014-06-20 18:24:01
【问题描述】:
到目前为止,我在更改 changeable_label 字段中的文本时遇到问题,当单击单选按钮“North-West”时,我无法使用文本将文本“Null”更改为 NW。我不知道我是否需要创建另一个“if”函数或什么...
#Import the Tkinter functions
from Tkinter import *
# Creating the windo
the_window = Tk()
# Title of window
the_window.title('Change colour')
# Label widget which has its properties modified
changeable_label = Label(the_window, text = 'Null',
font = ('Times', 48), fg = 'black')
# String variables which values change on selcetion of radio button
label_colour = StringVar()
# Creating an a function to change the labels text colour when the radio
# button is chosen
def change_colour():
if label_colour.get() == 'NW':
changeable_label['fg'] = 'black',
elif label_colour.get() == 'NE':
changeable_label['fg'] = 'green'
elif label_colour.get() == 'SW':
changeable_label['fg'] = 'blue'
else:
changeable_label['fg'] = 'yellow'
# Creating an a function to change the labels text when the radio
# button is chosen
# Creating the frame for the 4 buttons.
colour_buttons = Frame(the_window)
# Creating the Radio Buttons features
NW_button = Radiobutton(colour_buttons, text = 'North-West',
variable = label_colour, value = 'NW',
command = change_colour)
NE_button = Radiobutton(colour_buttons, text = 'North-East',
variable = label_colour, value = 'NE',
command = change_colour)
SW_button = Radiobutton(colour_buttons, text = 'South-West',
variable = label_colour, value = 'SW',
command = change_colour)
SE_button = Radiobutton(colour_buttons, text = 'South-East',
variable = label_colour, value = 'SE',
command = change_colour)
# Placing the 4 radio buttons on specific rows, columns, locations.
NW_button.grid(row = 1, column = 1, sticky = W)
NE_button.grid(row = 2, column = 1, sticky = W)
SW_button.grid(row = 1, column = 2, sticky = W)
SE_button.grid(row = 2, column = 2, sticky = W)
# Using the geomtry manager to pack the widgets onto the window.
margin = 8 # pixels
changeable_label.pack(padx = margin, pady = margin)
colour_buttons.pack(padx = margin, pady = margin)
# Start the event loop to react to user inputs
the_window.mainloop()
【问题讨论】:
-
我没有看到任何显示您尝试更改文本的代码。您正在更改颜色,并且更改文本以相同的方式完成,那么您在更改文本时遇到了什么问题?
标签: python tkinter radio-button