This is my first python personal project. I am wanting to use Tkinter to create a window (GUARDIAN LOCATOR) that asks the user to input a value (enter sailor guardian) into the entry box. The rest of the program is dependent on what the user types in the entry box as I will be having if/else statements reacting to the sailor guardian entered.
The issue I am having is storing what is entered in the entry box as a variable to use in my main file for the if/else statements. I can get the value to print to the prompt window, but I haven't been able to store it successfully to a global variable.
My Tkinter window is in it's own class.
I have tried many different ways of doing this based on similar issues from stackoverflow, but I am getting an error every time. This is my base code that still produces the error.
Class file with the Tkinter window
class GuardianLocator:
def __init__(self, master):
frame = Frame(master)
frame.grid()
master.title("GUARDIAN LOCATOR")
self.locator_label = Label(frame, text="Which Sailor Guardian are you looking for?", width=40, height=2)
self.locator_label.grid()
self.entry = Entry(frame)
self.entry.grid()
self.button1 = Button(frame, text="Search", command=self.guardian_name, pady=2)
self.button1.grid()
def guardian_name(self):
print(self.entry.get())
and in my main working file
root = Tk()
locator = guardian_locator.GuardianLocator(root)
root.mainloop()
This is my test loop to see if it's working.
if locator.guardian_input() is "Sailor Moon":
print("hi")
else:
print("no")
Not sure exactly how your code is organized and where is your "test loop" located, but I assume it is after root.mainloop(). Thus the script can be as follows:
from tkinter import *
class GuardianLocator:
def __init__(self, master):
self._name = ""
frame = Frame(master)
frame.grid()
master.title("GUARDIAN LOCATOR")
self.locator_label = Label(frame, text="Which Sailor Guardian are you looking for?", width=40, height=2)
self.locator_label.grid()
self.entry = Entry(frame)
self.entry.grid()
self.button1 = Button(frame, text="Search", command=self.guardian_name, pady=2)
self.button1.grid()
def guardian_name(self):
self._name = self.entry.get()
print(self.entry.get())
root = Tk()
locator = GuardianLocator(root)
root.mainloop()
# this will be executed after the root window is closed.
print("Name is", locator._name)
Please note self._name = "" in the constructor. This instance variable can be used to store the name provided in your GuardianLocator window.
Related
So far I have tried the advice from u/OA998
"""https://www.reddit.com/r/learnpython/comments/45h05k/solved_kernel_crashing_when_closing_gui_spyder/
My code is supposed to just open up a window with two labels and an entry field. The code works fine if I restart the Kernel but just running it after closing the window will result in an window popping up that has no text (from the StringVar). The third time it doesn't even open anymore. I'm not quite sure what causes this."""
Code:
""" create a GUI inside a class (this allows variables to be added and changed by several parts in the GUI). By using the “self” keyword we can access the attributes and methods of the class in python. It binds the attributes with the given arguments.
from tkinter import *
""" create a GUI inside a class (this allows variables to be added and changed by several parts in the GUI)"""
""" By using the “self” keyword we can access the attributes and methods of the class in python. It binds the attributes with the given arguments."""
class TESTGUI:
def __init__(self, window):
"""We will use an entry widget, and StringVar to keep track of the current text in the
box, and a label to display a message."""
"""labelpositioning"""
margin = 2
self.spacer = Label(window, width=margin, height=margin)
self.spacer.grid(column=0,row=0)
"""for toogle button create StringVar in tkinter class to hold the text"""
"""labeltexts"""
self.labelText_1 = StringVar()
self.labelText_1.set("begin experiment")
self.labelText_2 = StringVar()
self.labelText_2.set("calibrate")
"""labelformat"""
self.label = Label(window, textvariable=self.labelText_1, width=12, height=3, borderwidth=3, relief=SOLID)
self.label.grid(column=1, row=1)
self.label = Label(window, textvariable=self.labelText_2, width=12, height=3, borderwidth=3, relief=SOLID)
self.label.grid(column=2, row=1)
"""labelbutton"""
self.button = Button(window, text="press", command=self.pressed_button_1)
self.button.grid(column=1,row=2)
self.button = Button(window, text="press", command=self.pressed_button_2)
self.button.grid(column=2,row=2)
"""Entry(password)Label"""
self.entryLabel_1 = Label(window, text = "enter your password")
self.entryLabel_1.grid(column=0,row=3)
""" Next add a StringVar to hold the password and Entry box to type the password."""
""" The trace function will call a checkStrength() function when the StringVar is changed."""
self.password = StringVar()
self.password.trace("w", lambda name, index, mode, password=self.password:self.checkStrenght())
self.entry = Entry(window, textvariable=self.password)
self.entry.grid(column=1, row=3)
""" then create the StringVar to hold the strength string, and the label to display it. """
self.strenghtText = StringVar()
self.strenghtText.set("")
self.strenghtLabel = Label(window, textvariable=self.strenghtText, width=10)
self.strenghtLabel.grid(column=3, row=3)
"""CheckStrenghtFunctionOfEntry"""
def checkStrenght(self):
lenght = len(self.password.get())
if lenght == 0:
self.strenghtText.set("")
self.strenghtLabel.config(bg="SystemWindowBody")
elif lenght >= 1:
self.strenghtText.set("strong")
self.strenghtText.config(bg = "green3")
"""ButtonFunction"""
def pressed_button_1(self):
if self.labelText_1.get() == "begin experiment":
self.labelText_1.set("abort experiment")
else:
self.labelText_1.set("begin experiment")
def pressed_button_2(self):
if self.labelText_2.get() == "calibrate":
self.labelText_2.set("recalibrate")
else:
self.labelText_2.set("calibrate")
""" define Variables for window size """
width=300
height=300
""" define tk for used variable to use tkinter class """
if __name__ == "__main__":
window = Tk()
window.minsize(width, height)
window.title("Photonics Lab")
gui = TESTGUI(window)
window.mainloop()
I am quite new with Tkinter and am trying to create a new window using this script while keeping the current window but i am get the error
_init_() missing 1 required positional argument: 'parent'. I am not really sure what the reason is but I am assuming that the command function for my button isn't working the way I want it.
The script currently looks something like this:
from tkinter import simpledialog
from tkinter import *
class Additional(simpledialog.Dialog):
def body(self, master):
#input fields
Label(master, text="Picture 3 Path:").grid(row=1)
#input fields for tags
#add as needed
self.e1 = Entry(master)
self.e1.grid(row=1, column=1, ipadx=150)
return self.e1 # initial focus
def apply(self):
first = self.e1.get()
self.ttag1 = (first)
class Initial(simpledialog.Dialog):
def body(self, master):
#input fields for username and passwords
Label(master, text="Usernames:").grid(row=1),
self.e1 = Entry(master)
self.b1 = Button(master, text = "Add More", bg= 'grey', command= Additional)
self.b1.grid(row=6, column=2, ipadx=75)
self.e1.grid(row=1, column=1, columnspan=2, ipadx=50)
return self.e1 # initial focus
def apply(self):
first = self.e1.get()
self.tag1 = (first)
root = tk.Tk()
root.withdraw()
d = Initial(root)
toor = tk.Tk()
toor.withdraw()
I have tried changing it up but it seems that it's not working right. Any ideas?
When calling the Additional class through the button command, you are not specifying what the parent root should be, and therefore the class fails to initiate. You can solve this by passing the master using a lambda
self.b1 = Button(master, text="Add More", bg='grey', command=lambda: Additional(master))
I'm working on a project that recommends movies to a user through a GUI. However I'm new to python and tkinter and struggling with calling functions and variable to functions in general, as a test I want to print out two Entry widgets to the console. I've tried quite a few different approaches but something always seems to go wrong. Here is the relevant code for this test:
class App(Frame):
def print_it(self):
print(mn, nr)
def __init__(self, master):
Frame.__init__(self, master)
self.filename = None
movie_name = Entry(master)
movie_name.grid()
movie_name.delete(0, END)
movie_name.insert(0, "Enter Movie Name")
mn = movie_name.get()
num_recs = Entry(master)
num_recs.grid()
num_recs.delete(0, END)
num_recs.insert(0, "Enter Number of Movies")
nr = num_recs.get()
button1 = Button(self, text="Start", command=self.print_it)
button2 = Button(self, text="Exit", command=master.destroy)
button1.grid()
button2.grid()
self.grid()
root = Tk()
root.title("Recommender")
root.geometry("500x500")
app = App(root)
root.mainloop()
The variables nr and mn are obviously not being called in print it, ive tried the preifixes App. and self. but neither work. Can someone indicate to me what I need to do in order to call these variables. Thank you.
Good start.
First, if you want to access a variable from different methods, it needs to be named with the 'self.' prefix.
Second, the __init__ method runs when you boot, so if you call movie_name.get() it gets the value at boot time. You want the value it had when the button was pushed (I assume), so you need to move the get() call into the method that gets called when the button is pushed.
Third, an entry is empty when it's first initialized, you don't need to use the delete method to clear it.
class App(Frame):
def print_it(self):
print(self.movie_name.get(), self.num_recs.get())
def __init__(self, master):
Frame.__init__(self, master)
self.filename = None
self.movie_name = Entry(master)
self.movie_name.grid()
self.movie_name.insert(0, "Enter Movie Name")
self.num_recs = Entry(master)
self.num_recs.grid()
self.num_recs.insert(0, "Enter Number of Movies")
button1 = Button(self, text="Start", command=self.print_it)
button2 = Button(self, text="Exit", command=master.destroy)
button1.grid()
button2.grid()
self.grid()
root = Tk()
root.title("Recommender")
root.geometry("500x500")
app = App(root)
root.mainloop()
I am currently writing an application that uses Tkinter to provide a graphical interface for the user.
The app has been working very well and recently I decided to add some checkboxes, the idea being that when the user checks one of the boxes, another set of text is sent through an API.
I have input boxes that I am able to get to work perfectly, however for some reason whenever I try and retrieve the value of the checkbox, I get the following error:
if check.get():
NameError: name 'check' is not defined
For the life of me I can't figure out why this error is popping up, here is the rest of my code, to make it clearer I have removed the working code for the input boxes.
from tkinter import *
class GUI:
def __init__(self, master):
check = IntVar()
self.e = Checkbutton(root, text="check me", variable=check)
self.e.grid(row=4, column=2)
self.macro_button = Button(master, text="Test Button", command=self.test)
self.macro_button.grid(row=11, column=1)
def test(self):
if check.get():
print('its on')
else:
print('its off')
root = Tk()
root.resizable(width=False, height=False)
my_gui = GUI(root)
root.mainloop()
When I run this code, and press the button labeled 'test button', that is when the error appears in my terminal.
Anyone have any idea why this is happening for my checkboxes, and not for my inputboxes?
EDIT:
what's even odder for me is that this code that I found online made to teach you how to use a tkinter checkbox works like a charm, and its almost identical to mine:
import tkinter as tk
root = tk.Tk()
var = tk.IntVar()
cb = tk.Checkbutton(root, text="the lights are on", variable=var)
cb.pack()
def showstate():
if var.get():
print ("the lights are on")
else:
print ("the lights are off")
button = tk.Button(root, text="show state", command=showstate)
button.pack()
root.mainloop()
You just need to make check an instance variable with self.
I.E
class GUI:
def __init__(self, master):
self.check = IntVar()
self.e = Checkbutton(root, text="check me", variable=self.check)
self.e.grid(row=4, column=2)
self.macro_button = Button(master, text="Test Button", command=self.test)
self.macro_button.grid(row=11, column=1)
def test(self):
if self.check.get():
print('its on')
else:
print('its off')
root = Tk()
root.resizable(width=False, height=False)
my_gui = GUI(root)
root.mainloop()
The example you found online is written in an 'inline' style - which is fine until your GUI becomes larger and you require many methods and variables to be used / passed.
I'm going to create a program that resembles the image below. The interface uses one text entry for a name, one button, and two labels. The button should have the text Say hello and when the user clicks the button, the bottom label should display the name with Hi in front of it (see image below)
Here's what I've got
from tkinter import *
from tkinter.ttk import *
def say_hello():
name_var.set(name_entry.get())
def main():
global window, name_var, name_entry
window = Tk()
top_label = Label(window, text='Enter a name below')
top_label.grid(row=0, column=0)
name_var = StringVar()
name_entry = Entry(window, textvariable=name_var)
name_entry.grid(row=1, column=0)
hello_button = Button(window, text='Say hello', command=say_hello)
hello_button.grid(row=2, column=0)
bottom_label = Label(window, text='Hi ' + name_var)
bottom_label.grid(row=3, column=0)
window.mainloop()
main()
When I try to run it I get this error:
Traceback (most recent call last): File "C:\Program Files (x86)\Wing IDE 101 5.1\src\debug\tserver_sandbox.py", line 29, in <module> File "C:\Program Files (x86)\Wing IDE 101 5.1\src\debug\tserver_sandbox.py", line 24, in main builtins.TypeError: Can't convert 'StringVar' object to str implicitly
Everything works GUI wise, I'm just not sure how to get the last label that says "Hi Jack" to come up after pressing the button — i.e what my command should be in the hello_button line.
Here's your offensive code:
bottom_label = Label(window, text='Hi ' + name_var)
You can't really add a string and an instance of a class. A Tkinter StringVar isn't actually a string, but like a special thing for the gui to hold a string. That's why it can update automatically and stuff like that. Solution is simple:
bottom_label = Label(window, text = 'Hi ' + name_var.get())
Here's how I did it:
#!/usr/bin/env python2.7
import Tkinter as tk
class Application(tk.Frame):
def __init__(self, master=None):
self.name_var = tk.StringVar()
tk.Frame.__init__(self, master)
self.pack()
self.createWidgets()
def createWidgets(self):
self.top_label = tk.Label(self, text='Enter a name below')
self.top_label.grid(row=0, column=0)
self.name_entry = tk.Entry(self)
self.name_entry.grid(row=1, column=0)
self.hello_button = tk.Button(self, text='Say hello', command=self.say_hello)
self.hello_button.grid(row=2, column=0)
self.output = tk.Label(self, textvariable=self.name_var)
self.output.grid(row=3, column=0)
def say_hello(self):
self.name_var.set("Hi {}".format(self.name_entry.get()))
root = tk.Tk()
app = Application(master=root)
app.mainloop()
Ultimately it was very similar to your code. The only thing you were missing was how to use Tkinter.StringVar() correctly. You need to set the bottom label's textvariable to name_var when you create it, and then you should be good to go.
This simple class should do what you want:
from tkinter import Button, Tk, Entry,StringVar,Label
class App():
def __init__(self, **kw):
self.root = Tk()
# hold value for our output Label
self.s = StringVar()
# label above our Entry widget
self.l = Label(self.root, text="Enter name here").pack()
# will take user input
self.e = Entry(self.root)
self.e.pack()
self.b = Button(self.root, text="Say Hello",command=self.on_click).pack()
# textvariable points to our StringVar
self.l2 = Label(self.root, textvariable=self.s).pack()
self.root.mainloop()
# every time the Button is pressed we get here
# an update the StringVar with the text entered in the Entry box
def on_click(self):
self.s.set(self.e.get())
App()
You just need to create a couple of Labels, and Entry widget to take the name and a callback function to update the StringVar value so the label/name value gets updated.