tkinter: can't enter into entry widget

女生的网名这么多〃 提交于 2019-12-11 01:38:10

问题


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.

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.

Below is my code:

config.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()

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

回答1:


For anyone who comes here after me, my solution ended up being

root.overrideredirect(True)

Working fine on Windows, but causing this text entry problem on Mac.



来源:https://stackoverflow.com/questions/24499711/tkinter-cant-enter-into-entry-widget

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!