tkinter:无法进入入口小部件 [英] tkinter: can't enter into entry widget
问题描述
我不明白为什么我的代码中 rackGUI.py
下的输入框是静态的/不允许输入任何内容.我相信所有 Entry
对象都被正确实例化.我将文本变量指定为 StringVar()
的实例.我的直觉告诉我问题在于 create_button
实例化中的命令参数,但我不确定为什么.我认为通过设置 command = lambda:function
不会调用该函数.
I don't understand why the entry boxes under rackGUI.py
in my code are static/won't allow anything to be entered. I believe all the Entry
objects are instantiated correctly. I specified the textvariable as instances of the StringVar()
. My gut tells me the problem lies in command argument in create_button
instantiation but I'm not really sure why. I thought by setting command = lambda:function
the function would not be called.
点击菜单中的'New'
,main.py
成功调用rackGUI.create()
,成功调用input_form()
.单击按钮 'create_button'
成功调用 drawRack
,它打印到外壳 'test'
.我还添加了一个测试,其中我打印了每个输入框的值类型,即 print type(rack_name.get())
并且这成功地返回了 'str'
类型.
Upon clicking 'New'
in the menu, main.py
successfully calls rackGUI.create()
which successfully calls input_form()
. Clicking the button 'create_button'
successfully calls drawRack
which prints to the shell 'test'
. I also added a test where I printed the type of value for each entry box i.e., print type(rack_name.get())
and this successfully returns type 'str'
.
所以主要的问题还是输入框是静态的.
So again the main problem is that the entry box is static.
下面是我的代码:
配置文件
"""
config.py
"""
import Tkinter as tk
import tkMessageBox as tkmb
#setup
root = tk.Tk()
root.title("TLA Database Tool")
frame = tk.Frame(height = 300, width = 250)
frame.pack()
main.py
#main.py
from config import *
import rackGUI
def createRackTemplate():
rackGUI.create()
def loadRackTemplate():
rackGUI.load()
menubar = tk.Menu(root)
filemenu = tk.Menu(menubar)
filemenu.add_command(label = "New", command = createRackTemplate)
filemenu.add_command(label = "Load", command = loadRackTemplate)
menubar.add_cascade(label = "File", menu = filemenu)
tkmb.showinfo("Welcome", "Under File click New to create a new rack template.\n\
Click load to load rack template.")
root.config(menu = menubar)
root.mainloop()
rackGUI.py
"""
rackGUI.py
"""
from config import *
def input_form():
form_frame = tk.Frame(frame)
form_frame.pack()
tk.Label(form_frame, text = "Rack Template Name (e.g., Knox Type 4)").pack()
rack_name = tk.Entry(form_frame, textvariable = tk.StringVar())
rack_name.pack()
tk.Label(form_frame, text = "Dimensions").pack()
tk.Label(form_frame, text = "#rack rows").pack()
num_rack_rows = tk.Entry(form_frame, textvariable = tk.StringVar())
num_rack_rows.pack()
tk.Label(form_frame, text = "#nodes per row").pack()
num_slots = tk.Entry(form_frame, textvariable = tk.StringVar())
num_slots.pack()
create_button = tk.Button(form_frame, text = "Create!",\
command = lambda: drawRack(rack_name, num_rack_rows, num_slots))
create_button.pack()
def drawRack(rack_name, num_rack_rows, num_slots):
print rack_name.get(), num_rack_rows.get(), num_slots.get()
def create():
input_form()
def load():
pass
推荐答案
对于那些在我之后来到这里的人,我的解决方案最终是
For anyone who comes here after me, my solution ended up being
root.overrideredirect(True)
在 Windows 上工作正常,但在 Mac 上导致此文本输入问题.
Working fine on Windows, but causing this text entry problem on Mac.
这篇关于tkinter:无法进入入口小部件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!