input box does not appear in tkinter window - python

I have an issue with tkinter. Basically When I attempt to create an input box, the window opens with the title only.
from tkinter import *
window = Tk()
window.title("name generator")
def openInterface():
inputLabel = Label(window, text="Enter your name")
inputLabel.grid(row=0, column=2)
print(inputLabel)
Am i missing something? My apologies in advance for this is a noob question.

You never call your openInterface function. Functions are different from code in the global scope becasue they only get executed when called, not when they are defined.
from tkinter import *
window = Tk()
window.title("name generator")
openInterface()
def openInterface():
inputLabel = Label(window, text="Enter your name")
inputLabel.grid(row=0, column=2)
print(inputLabel)

I think you are missing two things, you haven't call
funtion and also you you need to use
window.mainloop()

Related

Python tkinter kept saying my variable to hold the .get() value is not defined

I am learning how to use the .get() for tkinter, and trying to write this basic GUI that can store, process, and display data depending on a user input.
Now (I am fairly new to this, so I am probably wrong) to my knowledge, I need to use the .get() and store it into a variable for future uses.
Now here are my codes, but when I run the code, it kept saying I did not define my variable in the function I defined.
I wrote in Pycharm, the variable I wrote in the first line of the function just keep turning grey.
Why is this happening, am I missing something important?
Sidenote:
I have done some research and saw some result regarding using the following method:
StringVar()
fstring, f"{}"
but I still can't figure out how that works and how it is affecting my code that Python is not accepting my variable.
Import tkinter as tk
def event():
expEntry = entry.get()
window = tk.Tk()
entry = tk.Entry(window)
button = tk.Button(window,commnad=event())
expEntry = tk.Label(window,text = expEntry)
entry.pack()
button.pack()
expEntry.pack()
window.mainloop()
You should use a StringVar to store the value of the entry when event() is called by button
import tkinter as tk
def event(): # use this function to update the StringVar
entry_content = entry.get()
entry_var.set(entry_content)
window = tk.Tk()
# declare a StringVar to store the value of your Entry and update your Label
entry_var = tk.StringVar(window, 'Default Text')
# use 'textvariable' here to update the label when the bound variable changes
expEntry = tk.Label(window, textvariable=entry_var)
entry = tk.Entry(window)
# there was a typo in 'command' here, FIY
button = tk.Button(window, text='Press me', command=event)
entry.pack()
button.pack()
expEntry.pack()
window.mainloop()
Note: you don't need the () for the command binding, as you're not calling the event function there! You're just telling the button the name of the function it should call.
Easier way to do this. Just added tk.StringVar().
import tkinter as tk
window = tk.Tk()
window.geometry("700x300")
window.title("StringVar Object in Entry Widget")
var =tk.StringVar(window)
def event():
Label2.config(text=var.get())
myEntry = tk.Entry(window, textvariable=var)
myEntry.pack()
myButton = tk.Button(window, text="Submit", command=event)
myButton.pack()
Label2 = tk.Label(window, font="Calibri,10")
Label2.pack()
window.mainloop()
Output before:
Output after:

Python: Reading from a spin box numbers and do following command of one

Good Morning/Evening,
I want to read a number from a spinbox, and if it is 2, it should print something. But my code does not work out. I've tried it with a slider instead of a spinbox and it worked out. But for me, it is really important to use a spinbox, so I hope somebody have an idea.
Code:
from tkinter import *
def a():
if spin.get()==2:
print("Hello World")
root = Tk()
root.geometry('300x100')
spin =Spinbox(root, from_=0, to=10,command=a)
button = Button(root, text='Enter')
button.pack(side=RIGHT)
spin.pack(side=RIGHT)
root.mainloop()
Adding to #coolCloud's answer I would suggest setting a textvariable for spinBox. So if the user changes it using the entry. It would automatically be updated.
Something like this:
from tkinter import *
def a(*event):
if text.get()=='2':
print("Hello World")
root = Tk()
root.geometry('300x100')
text = StringVar()
text.trace('w', a) # or give command=a in the button if you want it to call the event handler only when the button is pressed
spin =Spinbox(root, from_=0, to=10, textvariable=text)
button = Button(root, text='Enter')
button.pack(side=RIGHT)
spin.pack(side=RIGHT)
root.mainloop()

python entry widget and get method

Is there any way to put the textvariable in another variable and not have to use the ".get()"? I've been doing a lot of sifting thorugh tutorials and articles for what I realize is a very small issue but I'm probably misunderstanding something pretty key so i'm hoping someone can help me develop some intuition for the entry widget and .get() method.
Below is part of a script that I've been working on where I want to take the text entered in the entry box and use it later. I can use it if I use search_word.get(), but I don't why I can't do something like New_variable=search_word.get() so that from that point on I can just use "New_variable".
import tkinter as tk
from tkinter import *
from tkinter import ttk
Text_input_window = Tk()
Text_input_window.geometry('600x350+100+200')
Text_input_window.title("Test")
label_1=ttk.Label(Text_input_window, text="Enter word to search:", background="black", foreground="white")
label_1.grid(row=1, column=0, sticky=W)
search_word=StringVar()
entry_1=ttk.Entry(Text_input_window,textvariable=search_word, width=40, background="white")
entry_1.grid(row=2, column=0, sticky=W)
New_variable=StringVar()
New_variable=search_word.get()
def click():
print(New_variable)
print(search_word.get())
Text_input_window.destroy()
btn_1=ttk.Button(Text_input_window, text="submit", width=10, command=click)
btn_1.grid(row=3, column=0, sticky=W)
Text_input_window.mainloop()
Problem is not .get() but how all GUIs works.
mainloop() starts program so new_variable = search_word.get() is executed before you even see window - so it tries to get text before you put text in Entry.
You have to do it inside click() which is executed after you put text in entry and click button.
import tkinter as tk
# --- functions ---
def click():
global new_variable # inform function to use external/global variable instead of creating local one
#new_variable = entry.get() # you can get it directly from `Entry` without StringVar()
new_variable = search_word.get()
root.destroy()
# --- main ---
new_variable = '' # create global variable with default value
root = tk.Tk()
search_word = tk.StringVar()
entry = tk.Entry(root, textvariable=search_word)
entry.pack()
btn = tk.Button(root, text="submit", command=click)
btn.pack()
root.mainloop() # start program
# --- after closing window ---
print('new_variable:', new_variable)
print('search_word:', search_word.get()) # it seems it still exists
# print('entry:', entry.get()) # `Entry` doesn't exists after closing window so it gives error
Is there any way to put the textvariable in another variable and not have to use the ".get()"?
No, there is not. Tkinter variables are objects, not values. Anytime you want to use a value from a tkinter variable (StringVar, IntVar, etc) you must call the get method.

How to close other window by python Tkinter?

I have following python code in Tkinter.
import tkinter as tk
def main_gui(login, s):
login.quit() # close login window
win = tk.Tk()
win.geometry('300x150')
name = tk.Label(win, text='Hello' + s.get()) # Hello David
name.pack()
win.mainloop()
# initial Tkinter frame
login = tk.Tk()
login.title('Login')
login.geometry('300x150')
# input user name
user_name_var = tk.StringVar()
user_name_var.set('David')
tk.Label(login, text='User name').place(x=10, y=50)
user_name = tk.Entry(login, textvariable=user_name_var)
user_name.place(x=100, y=50)
input_ok = tk.Button(win_login, command=lambda: main_gui(login, user_name), text='OK', width=15)
input_ok.place(x=100, y=90)
win_login.mainloop()
I want to close login window, but my code can not close it. How to solve it.
You are almost there - only two details you have to adapt:
The method to remove a widget in Tkinter is destroy, so login.quit() should be login.destroy().
Once login is destroyed, the user_name Entry will also be destroyed, and you will not be able to get the name from it anymore. You should get the name earlier, e.g., directly in the lambda:
... lambda: main_gui(login, user_name.get()), ...
you can use the
root.withdraw()
function, this will close the window without completely destroying all of the root.after functions

Preventing circular import example

Lets say I have two python files. Both with an GUI. First is "Main" second is "Calculator". From Main I will start Calculator. So I have to import calculator. In Calculator I will do a calculation. Lets keep I easy an say 1+1=2. Now I want to "send" this Result to an Text in Main.
How do I do that without an circular import? I cant find an good tutorial/example for that!
My code so far:
Main:
from tkinter import *
import Test_2
window = Tk()
window.title("First Window")
def start():
Test_2.start_second()
Input1 = Entry(window)
Input1.grid(row=0,column=0, padx=10, pady=5)
Start = Button(window,text="Start", command=start)
Start.grid(row=1,column=0, padx=10, pady=5)
window.mainloop()
Second:
from tkinter import *
def start_second():
window2 = Tk()
window2.title("Second Window")
def send():
x = Input.get()
Input2 = Entry(window2)
Input2.grid(row=0,column=0, padx=10, pady=5)
Send = Button(window2,text="Send", command=send)
Send.grid(row=1,column=0, padx=10, pady=5)
window2.mainloop()
This code does exactly what you asked for (as opposed to what I suggested in the comment; but anyway, you either get a value from a module function or you send a reference for it to alter)
I tried to follow your structure.
Basically it is a matter of sending the parent window and the first entry as parameters to the second window creation function. Don't call mainloop two times, just once in the end, and use Toplevel for all other windows after the main Tk one. This is not to say that I like the use of an inner function and of the lambda, for readability, but lambdas are necessary in tkinter everytime you want to send parameters to a command callback, otherwise it will get called right way in command definition.
tkinter_w1.py (your main.py)
from tkinter import Tk, ttk
import tkinter as tk
from tkinter_w2 import open_window_2
root = Tk()
entry1 = ttk.Entry(root)
button1 = ttk.Button(root, text='Open Window 2',
command=lambda parent=root, entry=entry1:open_window_2(parent, entry))
entry1.pack()
button1.pack()
root.mainloop()
tkinter_w2.py (your Test_2.py)
from tkinter import Tk, ttk, Toplevel
import tkinter as tk
def open_window_2(parent, entry):
def send():
entry.delete(0,tk.END)
entry.insert(0,entry2.get())
window2 = Toplevel(parent)
entry2 = ttk.Entry(window2)
button2 = ttk.Button(window2, text='Send', command=send)
entry2.pack()
button2.pack()

Categories

Resources