我不明白为什么我的代码中 rackGUI.py 下的输入框是静态的/不允许输入任何内容。我相信所有 Entry 对象都已正确实例化。我将文本变量指定为 StringVar() 的实例。我的直觉告诉我问题在于 create_button 实例化中的命令参数,但我不太确定为什么。我认为通过设置 command = lambda:function 函数不会被调用。
单击菜单中的'New' 后,main.py 成功调用了 rackGUI.create(),后者成功调用了 input_form ()。单击按钮 'create_button' 成功调用 drawRack,它打印到 shell 'test'。我还添加了一个测试,在其中打印每个输入框的值类型,即 print type(rack_name.get()) 并成功返回类型 'str'。
所以主要问题还是在于输入框是静态的。
下面是我的代码:
配置.py
"""
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()
主要.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
请您参考如下方法:
对于任何在我之后来到这里的人,我的解决方案最终是
root.overrideredirect(True)
在 Windows 上工作正常,但在 Mac 上导致此文本输入问题。
