user3761743
user3761743

Reputation: 165

tkinter: can't enter into entry widget

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

Upvotes: 2

Views: 4908

Answers (3)

toyota Supra
toyota Supra

Reputation: 4537

tkinter: can't enter into entry widget

For Python 2.7 and for Python 3.x, you can comment in or comment out.

You had too many windows' garbage. In other, to prevent window garbage. That why you are not calling import tkinter

We be focus on Python 3.x, as I do not have Python 2.7.

  • Move import rackGUI inside createRackTemplate() function. This will prevent garbage window.

For main.py:

import tkinter as tk

root = tk.Tk()
root.withdraw()

from config import * #Python 3.x
#import tkMessageBox as tkmb Python 2.7
 

def createRackTemplate():
    import rackGUI
    rackGUI.create()
    
def loadRackTemplate():
    test_3.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)

#Python 3.x
messagebox.showinfo("Welcome", "Under File click New to create a new rack template.\n\
Click load to load rack template.")


#Comment in for Python 2.7
#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()

For config.py:

"""
config.py
"""

import tkinter as tk #Python 3.x
#import Tkinter as tk #Python 2.7

from tkinter import messagebox #Python 3.x
#import tkMessageBox as tkmb #Python 2.7

#setup
root = tk.Tk()
root.title("TLA Database Tool")
frame = tk.Frame(height = 300, width = 250)
frame.pack()

For rackGUI.py:

  • Add module import tkinter as tk and add frame = tk.Tk(). Because you are calling frame for every tkinter widgets

Code:

from config import *
import tkinter as tk


frame = tk.Tk()
...
...

Screenshot for Python 3.x:

enter image description here

Upvotes: 0

dieserniko
dieserniko

Reputation: 141

I actually found the problem there. The issue seems to be the focus of the windows, since you're using a messagebox.

In my script I just had put a root.update() before opening another window (in my case a filedialog) and everything worked fine. There's an already existing issue for that: https://bugs.python.org/issue42867#msg384785

Upvotes: 1

Dave
Dave

Reputation: 41

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.

Upvotes: 4

Related Questions