Python tkinter Entry 小部件状态通过单选按钮切换 [英] Python tkinter Entry widget status switch via Radio buttons

查看:135
本文介绍了Python tkinter Entry 小部件状态通过单选按钮切换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个简单的问题(对于像我这样的 tkinter 新手来说不是那么简单):我正在构建一个 GUI,我想要两个单选按钮来驱动 Entry 小部件的状态(启用或禁用),用户将进入输入数据.当按下第一个单选按钮时,我希望条目被禁用;当按下第二个单选按钮时,我希望条目被禁用.

a simple question (not so simple for a tkinter newby like me): I'm building a GUI and I want to have two radio buttons driving the status (enabled or disabled) of an Entry widget, into which the user will input data. When the first radio button is pressed, I want the Entry to be disabled; when the second radio button is pressed, I want the Entry to be disabled.

这是我的代码:

from Tkinter import *

root = Tk()
frame = Frame(root)

#callbacks
def enableEntry():
    entry.configure(state=ENABLED)
    entry.update()

def disableEntry():
    entry.configure(state=DISABLED)
    entry.update()

#GUI widgets
entry = Entry(frame, width=80)
entry.pack(side='right')

var = StringVar()
disableEntryRadioButton = Radiobutton(frame, text="Disable", variable=var, value="0", command=disableEntry)
disableEntryRadioButton.pack(anchor=W)
enableEntryRadioButton = Radiobutton(frame, text="Enable", variable=var, value="1", command=enableEntry)
enableEntryRadioButton.pack(anchor=W)

我的想法是在按下每个单选按钮时调用正确的回调.但我不太确定它是否真的发生在我写的代码中,因为当我选择收音机时,条目的状态没有切换.

My idea is to invoke the proper callbacks when each radio button is pressed. But I'm not pretty sure that it actually happens with the code I wrote,because when I select the radios the status of the Entry is not switched.

我哪里错了?

推荐答案

你的程序有一些问题,但总体结构没问题.

You have a few things wrong with your program, but the general structure is OK.

  1. 你不是在调用 root.mainloop().这是事件循环为按钮点击等事件提供服务所必需的.
  2. 您使用 ENABLEDDISABLED 但不要在任何地方定义或导入它们.我个人更喜欢使用字符串值 "normal""disabled".
  3. 你没有打包你的主要 frame 小部件
  1. you aren't calling root.mainloop(). This is necessary for the event loop to service events such as button clicks, etc.
  2. you use ENABLED and DISABLED but don't define or import those anywhere. Personally I prefer to use the string values "normal" and "disabled".
  3. you aren't packing your main frame widget

当我修复这三件事时,您的代码工作正常.这是工作代码:

When I fix those three things your code works fine. Here's the working code:

from Tkinter import *

root = Tk()
frame = Frame(root)
frame.pack()

#callbacks
def enableEntry():
    entry.configure(state="normal")
    entry.update()

def disableEntry():
    entry.configure(state="disabled")
    entry.update()

#GUI widgets
entry = Entry(frame, width=80)
entry.pack(side='right')

var = StringVar()
disableEntryRadioButton = Radiobutton(frame, text="Disable", variable=var, value="0", command=disableEntry)
disableEntryRadioButton.pack(anchor=W)
enableEntryRadioButton = Radiobutton(frame, text="Enable", variable=var, value="1", command=enableEntry)
enableEntryRadioButton.pack(anchor=W)

root.mainloop()

这篇关于Python tkinter Entry 小部件状态通过单选按钮切换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆