Tkinter Reading Input Value from PopUp - python

Creating a popup window which will ask for email entry and then print the email when 'OK' is pressed, this is my code:
import tkinter as tk
class PopUp(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
tk.Label(self, text="Main Window").pack()
popup = tk.Toplevel(self)
popup.wm_title("EMAIL")
popup.tkraise(self)
tk.Label(popup, text="Please Enter Email Address").pack(side="left", fill="x", pady=10, padx=10)
self.entry = tk.Entry(popup, bd=5, width=35).pack(side="left", fill="x")
self.button = tk.Button(popup, text="OK", command=self.on_button)
self.button.pack()
def on_button(self):
print(self.entry.get())
app = PopUp()
app.mainloop()
Every time I run it I get this error:
AttributeError: 'NoneType' object has no attribute 'get'
The pop up works how it should but its the input entry that doesn't seem to be working.
I've seen this example before but it wasn't in the popup (I can get it work perfectly without the popup).
Any help is appreciated.

You can store the value in a StringVar variable and get() its value.
import tkinter as tk
from tkinter import StringVar
class PopUp(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
tk.Label(self, text="Main Window").pack()
popup = tk.Toplevel(self)
popup.wm_title("EMAIL")
popup.tkraise(self)
tk.Label(popup, text="Please Enter Email Address").pack(side="left", fill="x", pady=10, padx=10)
self.mystring = tk.StringVar(popup)
self.entry = tk.Entry(popup,textvariable = self.mystring, bd=5, width=35).pack(side="left", fill="x")
self.button = tk.Button(popup, text="OK", command=self.on_button)
self.button.pack()
def on_button(self):
print(self.mystring.get())
app = PopUp()
app.mainloop()

Related

tkinter: LabelFrame in a separate TopLevel window

My "simple" intention is to create a Dialog - launched by a button - where I have several LabelFrames with their radio button.
I tried with 1 LabelFrame with this simple code the radios appear in the main window, not in the dialogue one !!!! I cannot understand why. Please help.. ty for your attention
Here's the code:
import tkinter as tk
from tkinter import ttk
class PersonEmptyDocsDialog(tk.Toplevel):
def __init__(self, root,personid):
super().__init__(root)
self.personid = personid
self.code = tk.StringVar()
frame1 = ttk.Labelframe(self,text='testo').grid(column=0,row=0,padx=20,pady=20)
ttk.Radiobutton(frame1, text="Option 1", variable=self.code, value="0-0-0").pack()
ttk.Radiobutton(frame1, text="Option 1", variable=self.code, value="0-0-1").pack()
ttk.Radiobutton(frame1, text="Option 1", variable=self.code, value="0-0-2").pack()
self.ok_button = tk.Button(self, text="OK", command=self.on_ok).grid(column=0,row=1)
#self.ok_button.pack()
def on_ok(self, event=None):
self.destroy()
def show(self):
self.wm_deiconify()
self.wait_window()
return self.code.get()
class Example():
def __init__(self, root):
mainframe = ttk.Frame(root).pack(fill="both", expand=True)
self.root = root
ttk.Button(mainframe, text="Get Input", command=self.on_button).pack(padx=8, pady=8)
ttk.Label(mainframe, text="", width=20).pack(side="bottom", fill="both", expand=True)
def on_button(self):
string = PersonEmptyDocsDialog(self.root, 12).show()
print(string)
root = tk.Tk()
root.wm_geometry("400x200")
Example(root)
root.mainloop()
You cannot put in one line. You need to break this into in line 11
frame1 = ttk.Labelframe(self,text='testo')
frame1.grid(column=0,row=0,padx=20,pady=20)
Output:
Output after clicking Get input button:

Create label in tkinter and update the Label with an int variable

I want to create a label and update it with the int-value, which is updated by pressing the buttons, also in the label. I'm still new to Python and would like some help :)
import tkinter as tk
class Main(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
self.integer = tk.IntVar()
self.integer.set(0)
tk.Button(self, text='Quit', command=self.destroy).pack()
tk.Button(self, text='+', command=self.plus_one).pack()
tk.Button(self, text='-', command=self.take_one).pack()
self.entry0 = tk.Entry(self, textvariable=str(self.integer), justify="center", width=4)
self.entry0.pack()
def plus_one(self):
x = self.integer.get() + 1
self.integer.set(x)
def take_one(self):
x = self.integer.get() - 1
self.integer.set(x)
app = Main()
app.mainloop()
You would do this the same way you did with the Entry widget:
import tkinter as tk
class Main(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
self.integer = tk.IntVar()
self.integer.set(0)
tk.Button(self, text='Quit', command=self.destroy).pack()
tk.Button(self, text='+', command=self.plus_one).pack()
tk.Button(self, text='-', command=self.take_one).pack()
self.entry0 = tk.Entry(self, textvariable=str(self.integer), justify="center", width=4)
self.entry0.pack()
self.label0 = tk.Label(self, textvariable=str(self.integer))
self.label0.pack()
def plus_one(self):
x = self.integer.get() + 1
self.integer.set(x)
def take_one(self):
x = self.integer.get() - 1
self.integer.set(x)
app = Main()
app.mainloop()
As per your comments, if you are interested in having the binding at button press instead of button release, this has been already addressed here.

opening a new gui from the gui

updated code as per below comments
I'm struggling to get my top level to open on button press!
I've scanned the code on here but don't seem to be able to get a woking solution. My latest error is:
AttributeError: '_tkinter.tkapp' object has no attribute 'unavail'
from tkinter import *
from ScheduleApi import flightData
import config
from itinerary import fltCreate
class FrontEnd:
def __init__(self, master):
self.master = master
master.title("A simple GUI")
self.label = Label(master, text="This is our first GUI!")
self.label.grid()
self.greet_button = Button(master, text="Create Itinerary", command=self.ItinBuilder)
self.greet_button.grid(row=1)
self.close_button = Button(master, text="Close", command=master.quit)
self.close_button.grid(row=2)
def greet(self):
print("Greetings!")
def ItinBuilder(self):
self = Toplevel(self.master)
self.title ("Please build your itinerary")
self.addflt_button = Button(self.master, text="add flights", command=fltCreate)
self.addflt_button.grid(row=1)
self.addfhtl_button = Button(self.master, text="add hotel", command=self.master.unavail)
self.addflt_button.grid(row=1, column=1)
self.addfmsc_button = Button(self.master, text="add misc item", command=self.master.unavail)
self.addflt_button.grid(row=2, column=1)
self.prvitin_button = Button(self.master, text="preview", command=self.master.unavail)
self.addflt_button.grid(row=2, column=1)
def unavail(self, Toplevel):
print("Function not yet available.")
root = Tk()
my_gui = FrontEnd(root)
root.mainloop()
Please change your function ItinBuilder to this. I hope this is what you expect.
def ItinBuilder(self):
self.newWindow = Toplevel(self.master)
self.newWindow.title ("Please build your itinerary")
self.addflt_button = Button(self.newWindow, text="add flights", command=fltCreate)
self.addflt_button.grid(row=0)
self.addfhtl_button = Button(self.newWindow, text="add hotel", command=self.master.unavail)
self.addfhtl_button.grid(row=0, column=1)
self.addfmsc_button = Button(self.newWindow, text="add misc item", command=self.master.unavail)
self.addfmsc_button.grid(row=1, column=0)
self.prvitin_button = Button(self.newWindow, text="preview", command=self.master.unavail)
self.prvitin_button.grid(row=1, column=1)
The add flights button is in main GUI because you passed the reference to your main window self.master as first argument(called parent in tkinter) in your code.
Only add_flights button is seen and not others because you called grid method only for self.addflt_button and not for other buttons(missed to change variable names in other calls I guess).

Problem with tkinter and urllib for checking websites

I am trying to create an application that will allow you to put in multiple websites and check if they are down. When I run the code the window opens and everything works fine until i click search. Then i get the following error
AttributeError: 'Window' object has no attribute 'text_entry'
Any help would be greatly appreciated
#Import everything from tkinter
from tkinter import *
import urllib.request
#Main window
class Window(Frame):
#Master Widget
def __init__(self, master = None):
Frame.__init__(self, master)
self.master = master
self.init_window()
#Creation of init_window
def init_window(self):
# changing the title of our master widget
self.master.title("GUI")
# allowing the widget to take the full space of the root window
self.pack(fill=BOTH, expand=1)
#Menu
menu = Menu(self.master)
self.master.config(menu=menu)
#File Menu option
file = Menu(menu)
file.add_command(label="Exit", command=self.client_exit)
menu.add_cascade(label="File", menu=file)
#Text Box
text_entry = Entry(self, width=20, bg="white")
text_entry.place(x=0, y=0)
#Submit button
searchButton = Button(self, text='SUBMIT', width=6,
command=self.search)
searchButton.place(x=200, y=30)
#Output Box
output = Text(self, width=20, height=1, bg="white")
output.place(x=0, y=50)
def search(self):
entered_text = self.text_entry.get()
output.delete(0.0, END)
definition=(int(urllib.request.urlopen(entered_text).getcode()))
output.insert(END, definition)
root = Tk()
#Size of the window
root.geometry("400x300")
#Window instance
app = Window(root)
#Show and mainloop
root.mainloop()
You haven't put text_entry on the self object and you can't access it in search function.
#Text Box
self.text_entry = Entry(self, width=20, bg="white")
self.text_entry.place(x=0, y=0)

Nothing coming up in a GUI (Tkinter) window

I am writing a code for a login system using tkinter and for some reason when I run the code there are no error messages and a window pops up but without the title, buttons or labels I need.
from tkinter import *
import tkinter.messagebox
frame = Tk()
def adminlogincheck(self, master):
frame = Frame(master)
frame.pack()
if username == '123key' and password == 'key123':
accept = Label(frame, text='Login Successful')
else:
decline = Label(frame, text='Login incorrect')
mainloop()
def adminselect(self, master):
frame = Frame(master)
frame.pack()
self.button = Button(frame, text="Cancel", fg="red", command=quit)
self.button.pack(side=LEFT)
self.slogan = Button(frame, text="Proceed", command=self.adminlogin)
self.slogan.pack(side=LEFT)
mainloop()
def adminlogin(self, master):
frame = Frame(master)
frame.pack()
username_entry = Entry(frame)
password_entry = Entrey(frame)
confirm = Button(frame, text='Login', command = adminlogincheck)
loginquit = Button(frame, text='Cancel', command=quit)
mainloop()
I will add more after the login system works but does anyone know why no buttons or labels appear?
There's enough in your request to see what you're trying to accomplish, but there are many issues with the code. Here is a working model of what you appear to be working toward...
from tkinter import *
import tkinter.messagebox
class Admin:
def __init__(self, master):
self.frame = Frame(master)
self.frame.pack()
self.username = StringVar()
self.password = StringVar()
def logincheck(self):
self.clearframe()
if self.username.get() == '123key' and self.password.get() == 'key123':
accept = Label(self.frame, text='Login Successful')
accept.pack(side=LEFT)
else:
decline = Label(self.frame, text='Login incorrect')
decline.pack(side=LEFT)
def select(self):
self.clearframe()
self.button = Button(self.frame, text="Cancel", fg="red", command=quit)
self.button.pack(side=LEFT)
self.slogan = Button(self.frame, text="Proceed", command=self.adminlogin)
self.slogan.pack(side=LEFT)
def login(self):
self.clearframe()
username_entry = Entry(self.frame, textvariable=self.username)
username_entry.pack()
password_entry = Entry(self.frame, textvariable=self.password)
password_entry.pack()
confirm = Button(self.frame, text='Login', command = self.logincheck)
confirm.pack()
loginquit = Button(self.frame, text='Cancel', command=quit)
loginquit.pack()
def clearframe(self):
# Destroy all children of the class's frame.
for child in self.frame.winfo_children():
child.destroy()
root = Tk()
admin = Admin(root)
admin.login()
mainloop()

Categories