【问题标题】:Changing text using Tinker Python使用 Tkinter Python 更改文本
【发布时间】: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


【解决方案1】:

试试

changeable_label = Label(the_window, textvariable = label_colour)

让标签显示单选选项的文本。
我会重命名label_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'

会变成

label_foreground = {'NW': 'black', "NE" : 'green', 'SW' : 'blue', 'SE' : 'yellow'}
changeable_label['fg'] = label_foreground[label_colour.get()]

如果您愿意,您可以对文本执行相同的操作。阅读help(dict)

【讨论】:

  • 为帮助干杯,我刚刚添加了另一个 def。
【解决方案2】:

回答了我自己的问题,我不敢相信我 之前没看到...

    def change_text():
if label_text.get() == 'NW':
    changeable_label['text'] = 'NW'
elif label_text.get() == 'NE':
    changeable_label['text'] = 'NE'
elif label_text.get() == 'SW':
    changeable_label['text'] = 'SW'
else:
    changeable_label['text'] = 'SE'

【讨论】:

  • 考虑使用字典或列表。创建它们是为了替换大型 if 构造。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-01
相关资源
最近更新 更多