name 'main_window' is not defined - python-3.x

from tkinter import *
def create_main_window():
global main_window
main_window = Toplevel()
main_window.update()
entrance_window = Tk()
first_text_label = Label(entrance_window, text="you are in:").grid(row=0, column=0)
place_entry = Entry(entrance_window).grid(row=0, column=1)
submit_button = Button(entrance_window, text="Submit", command=create_main_window).grid(row=1, column=0, columnspan=2)
Label(main_window, text=f"{place_entry}").pack()
entrance_window.mainloop()
the program should open a new window with the text from the entry box from the first window but it either shows None if I write
Label(main_window, text=f"{place_entry}").pack()
in the create_main_window or it gives me an error saying that main_window is not defined if I write it after the button code.
Can someone help with this?

Try this:
from tkinter import *
def create_main_window():
global main_window
main_window = Toplevel(main_window)
label = Label(main_window, text=f"{place_entry.get()}")
label.pack()
# main_window.update() # This is useless
entrance_window = Tk()
first_text_label = Label(entrance_window, text="You are in:")
first_text_label.grid(row=0, column=0)
place_entry = Entry(entrance_window)
place_entry.grid(row=0, column=1)
submit_button = Button(entrance_window, text="Submit", command=create_main_window)
submit_button.grid(row=1, column=0, columnspan=2)
entrance_window.mainloop()
I moved the label creation inside create_main_window. Also please note that using var = a().b(), saves what ever b() returns inside var. That is why when you use var = Entry(...).pack(...), var is always None.

This is because you are trying to add a Label to an object that doesn't exist. Move the Label function to the create_main_window() function, like below:
from tkinter import *
def create_main_window():
global main_window, entrance_window
main_window = Toplevel()
place_entry = Entry(entrance_window).grid(row=0, column=1)
Label(main_window, text=f"{place_entry}").pack()
main_window.update()
entrance_window = Tk()
first_text_label = Label(entrance_window, text="you are in:").grid(row=0, column=0)
submit_button = Button(entrance_window, text="Submit", command=create_main_window).grid(row=1, column=0, columnspan=2)
entrance_window.mainloop()

Related

Python/tkinter - Getting the current value of the checkbox variable

I'd like to print the current value of the checkbox variable. Instead I get the last before the click. what am I doing wrong?
import tkinter as tk
def widget(frame):
chbx_value = 0
widget_col_span = 1
# widgets checkbox
var_c = tk.IntVar(master=frame, value=chbx_value)
widget_c = tk.Checkbutton(master=frame, text='', variable=var_c)
widget_c.bind("<ButtonRelease>", lambda event: print(var_c.get()))
widget_c.grid(row=0, column=0, columnspan=widget_col_span, padx=1, pady=1, sticky="ns")
root = tk.Tk()
root.title('My Window')
widget(root)
root.mainloop()
The event <ButtonRelease> obviously occurs before your variable has changed. My advice would be to use the command kwarg in the Checkbutton constructor. Besides, it probably makes more sense to use a boolean as variable:
import tkinter as tk
def widget(frame):
widget_col_span = 1
# widgets checkbox
var_c = tk.BooleanVar(master=frame)
widget_c = tk.Checkbutton(master=frame, text='', variable=var_c, command=lambda: print(var_c.get()))
widget_c.grid(row=0, column=0, columnspan=widget_col_span, padx=1, pady=1, sticky="ns")
root = tk.Tk()
root.title('My Window')
widget(root)
root.mainloop()

Show tkinter widget when checkbox is checked

I'm trying to make the entry widget show once the checkbutton is checked and hide when it's not.
from tkinter import *
master = Tk()
master.geometry('500x400')
other = IntVar()
Checkbutton(master, text="Other", variable=other, command=toggle()).grid(row=10, sticky=W)
def toggle():
if other.get()==1:
Entry(master,width=50).grid(row=11, sticky=W)
else:
Entry(master,width=50).grid_remove()
Try this:
from tkinter import *
def toggle():
if other.get():
ent.grid(row=11, sticky=W)
else:
ent.grid_forget()
master = Tk()
master.geometry('500x400')
other = BooleanVar()
Checkbutton(master, text="Other", variable=other, command=toggle).grid(row=10, sticky=W)
ent=Entry(master,width=50)
master.mainloop()

Confused about binding the enter key in tkinter

This is a search bar program and once enter is hit it will open google with whatever I searched:
import tkinter as tk
from tkinter import ttk
import webbrowser
root = tk.Tk()
root.title("Search Bar")
label1 = ttk.Label(root, text="Query")
label1.grid(row=0, column=0)
entry1 = ttk.Entry(root, width=50)
entry1.grid(row=0, column=1)
def callback():
webbrowser.open("http://google.com/search?q="+entry1.get())
def get(event):
webbrowser.open("http://google.com/search?q=" + entry1.get())
button1 = ttk.Button(root, text="Search", width=10, command=callback)
button1.grid(row=0, column=2)
entry1.bind("<Return>", get)
root.mainloop()
What I'm most confused about is why did I need a second function [get(event)] in order to bind the enter key at entry1.bind("<Return>", get). Why couldn't I just put entry1.bind("<Return>", callback) (which is for the button). For some reason, the enter bind function requires a parameter and I'd just like an explanation as to why that is? Even though whatever is in the parameter is not being called.
You can use event=None in
def callback(event=None):
and then you can use with command= and bind()
bind() will run it with event, command= will run it without event and it will use None
import tkinter as tk
from tkinter import ttk
import webbrowser
def callback(event=None):
webbrowser.open("http://google.com/search?q="+entry1.get())
root = tk.Tk()
root.title("Search Bar")
label1 = ttk.Label(root, text="Query")
label1.grid(row=0, column=0)
entry1 = ttk.Entry(root, width=50)
entry1.grid(row=0, column=1)
button1 = ttk.Button(root, text="Search", width=10, command=callback)
button1.grid(row=0, column=2)
entry1.bind("<Return>", callback)
root.mainloop()
bind() can be used with different events and objects so it send this information to function - ie. event.widget - so you can bind the same function to different objects.
def callback(event=None):
print(event)
if event: # if not None
print(event.widget)
You can use
def callback(event=None):
Or u can pass None as parameter
import tkinter as tk
from tkinter import ttk
import webbrowser
root = tk.Tk()
root.title("Search Bar")
label1 = ttk.Label(root, text="Query")
label1.grid(row=0, column=0)
entry1 = ttk.Entry(root, width=50)
entry1.grid(row=0, column=1)
def callback():
webbrowser.open("http://google.com/search?q="+entry1.get())
def get(event):
webbrowser.open("http://google.com/search?q=" + entry1.get())
button1 = ttk.Button(root, text="Search", width=10, command=lambda x=None:get(x))
button1.grid(row=0, column=2)
entry1.bind("<Return>", get)
root.mainloop()

I am having trouble making an input text box that goes through a function and outputs test to an output textbox with Tkinter

im new to Python3.6.2
So I want a program that takes an input (Via text box from tkinter) and outputs a word in my custom "language"
with this function
def Mescre(n):
Words = (n)
Mes = str.maketrans('abcdefghijklmnopqrstuvwxyz', 'ektnopzcamjqwyuxsbfdiglhrv')
print(Words.translate(Mes))
and here's what i want the window to look like
from tkinter import*
root = Tk()
Mescre = Label(root, text="Input:")
English = Label(root , text="Output:")
label1.grid(row=0, sticky=E)
label2.grid(row=1, sticky=E)
entry1 = Entry(root)
entry2 = Entry(root)
entry1.grid = (row=0, column=1)
entry2.grid = (row=1, column=1)
root.mainloop()
if "hello" was in the Input text box, i want the output to be "coqqu" in the Output text box.
See my example below:
from tkinter import *
class App:
def __init__(self, root):
self.root = root
self.sv = StringVar()
self.Mes = str.maketrans('abcdefghijklmnopqrstuvwxyz', 'ektnopzcamjqwyuxsbfdiglhrv')
self.entry = Entry(self.root, textvariable = self.sv)
self.label = Label(self.root)
self.entry.pack()
self.label.pack()
self.sv.trace("w", self.callback)
def callback(self, *args):
self.label.configure({"text": self.entry.get().translate(self.Mes)})
root = Tk()
App(root)
root.mainloop()
Here we define a StringVar() to be the value of the attribute textvariable for the Entry widget.
We then assign a callback to a trace() on the variable so that whenever the variable is updated (When someone types in the Entry) we call callback().
Within callback() we use configure() on the Label widget in order to set the text to equal the post translation version of the value of the Entry widget.
This creates a "live updating" translation effect.
Here's a basic example:
import tkinter as tk
root = tk.Tk()
def Mescre():
val = textfield.get()
Words = (val)
Mes = str.maketrans('abcdefghijklmnopqrstuvwxyz', 'ektnopzcamjqwyuxsbfdiglhrv')
print(Words.translate(Mes))
textfield = tk.Entry(root)
textfield.pack()
button = tk.Button(root, command=Mescre, text='Push')
button.pack()
root.mainloop()
Updated:
import tkinter as tk
root = tk.Tk()
def Mescre():
val = textfield.get()
Words = (val)
Mes = str.maketrans('abcdefghijklmnopqrstuvwxyz', 'ektnopzcamjqwyuxsbfdiglhrv')
translation = Words.translate(Mes)
#print(translation)
outputfield.delete(0, tk.END)
outputfield.insert(0, translation)
textfield = tk.Entry(root)
textfield.pack()
outputfield = tk.Entry(root)
outputfield.pack()
button = tk.Button(root, command=Mescre, text='Push')
button.pack()
root.mainloop()

Return Value from TkInter Entry with Button

Python novice, here. I've noticed there are a lot of questions around the topic of returning values from a TkInter function, but none of the solutions seem to solve my issue.
I can successfully print self.e1path to the shell from within getPath.submit, but I cannot return it to the rest of my code. I'm using a print statement outside of the class to test whether I've successfully returned the CSV path.
from tkinter import *
import tkinter as tk
class getPath(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
self.label1 = tk.Label(self, text="CSV Path").grid(row=0, column=0)
self.e1 = tk.Entry(self, width=50)
self.e1Grid = self.e1.grid(row=0, column=1)
self.browse = tk.Button(self, text='Browse', command=self.getCSV).grid(row=0, column=2)
self.submit = tk.Button(self, text='Submit', command=self.submit).grid(row=1, column=1)
def getCSV(self):
self.fileName = filedialog.askopenfilename( filetypes = (('Comma Separated Values', '*.csv'), ('All Files', '*.*')), title = "Choose a CSV File")
self.e1.insert(10, self.fileName)
def submit(self):
self.e1Path = self.e1.get()
return self.e1Path
app = getPath()
app.mainloop()
print(app)
I figured it out! I needed to add a self.destroy() to the submit function. This stopped the mainloop and let me call on self.e1path outside of the function using app.e1path. New code:
from tkinter import *
import tkinter as tk
class getPath(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
self.label1 = tk.Label(self, text="CSV Path").grid(row=0, column=0)
self.e1 = tk.Entry(self, width=50)
self.e1Grid = self.e1.grid(row=0, column=1)
self.browse = tk.Button(self, text='Browse', command=self.getCSV).grid(row=0, column=2)
self.submit = tk.Button(self, text='Submit', command=self.submit).grid(row=1, column=1)
def getCSV(self):
self.fileName = filedialog.askopenfilename( filetypes = (('Comma Separated Values', '*.csv'), ('All Files', '*.*')), title = "Choose a CSV File")
self.e1.insert(10, self.fileName)
def submit(self):
self.e1Path = self.e1.get()
self.destroy()
app = getPath()
app.mainloop()
print(app.e1Path)

Resources