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)
Related
I am currently trying to learn tkinter. I do not understand why the buttons I defined do not appear in this code:
from tkinter import *
class Window(Frame):
def __init__(self, master=None):
Frame.__init__(self, master)
self.master = master
button1 = Button(self, text="Exit", width=12, command=self.clickButton1)
button1.grid(row=0)
button2 = Button(self, text="Test", width=12, command=self.clickButton2)
button2.grid(row=1)
def clickButton1(self):
exit()
def clickButton2(self):
print("Nice")
root = Tk()
app = Window(root)
root.title("Tkinter window")
root.mainloop()
When I don't use a class it works. Like this for example:
from tkinter import *
root = Tk()
button1 = Button(root, text="Works!!!")
button1.grid(row=0)
button2 = Button(root, text="Also works!!!")
button2.grid(row=1)
root.mainloop()
ยดยดยด
The reason is that the class creates a frame, and then puts widgets inside the frame. However, you never add the frame to the window so the widgets inside the frame will be invisible.
You need to make sure and call pack, grid, or place on the instance of Window, just like you do with any other widget.
app = Window(root)
app.pack(fill="both", expand=True)
Attempting to create a menu box but I am having trouble adding multiple buttons, there is only a "Quit" button visible when I run the code, please assist xx
class Menu(Frame):
def __init__(self, master = None):
Frame.__init__(self, master)
self.master = master
self.init_menu()
def init_menu(self):
self.master.title("DUNGEON MENU")
self.pack(expand = True, fill=BOTH)
quitB = Button(self, text = "Quit", fg = "red", command = self.client_exit)
quitB.grid(row = 0, column = 3, padx = 120)
def client_exit(self):
exit()
lootB = Button(self, text = "Loot", command = None)
lootB.grid(row = 1, column = 3, padx = 120)
root = Tk()
root.geometry("300x300")
app = Menu(root)
root.mainloop()
button.pack()```
I have created a working version from your code. You can find my finding in the below code as comments.
I am not totally sure what your expectation about your code. Now the "Quit" and "Loot" buttons are visible on the Frame and if you click to "Quit" button, the program will be end (Loot button does nothing).
Code:
from tkinter import Frame, BOTH, Button, Tk
class Menu(Frame):
def __init__(self, master=None):
Frame.__init__(self, master)
self.master = master
self.init_menu()
# You have to call the "client_exit" method to rendering the "Loot" button to Frame.
self.client_exit()
def init_menu(self):
self.master.title("DUNGEON MENU")
self.pack(expand=True, fill=BOTH)
# Destroy the master if you click to the "Quit" button.
quitB = Button(self, text="Quit", fg="red", command=lambda: self.master.destroy())
quitB.grid(row=0, column=0, padx=120)
def client_exit(self):
# exit() # This exit is not needed because it breaks the program running.
# You can define the call-back of button in "command" parameter of Button object.
lootB = Button(self, text="Loot", command=None)
lootB.grid(row=1, column=0, padx=120)
root = Tk()
root.geometry("300x300")
app = Menu(root)
root.mainloop()
# button.pack() # It does not have effect. You cannot define widgets after "mainloop"
Output:
>>> python3 test.py
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).
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()
Here is my code so far for a GUI I am making in tkinter, I have attempted to change the background colour of the window but it doesn't seem to work.
from tkinter import *
from tkinter.font import Font
class Window(Frame):
def __init__(self, master = None):
Frame.__init__(self, master)
self.master = master
self.init_window()
def init_window(self):
self.master.title("Main Menu")
self.pack(fill=BOTH, expand=1)
videoButton = Button(self, text="Video Mode", font=helv18, bg="white", height=1, width=18)
videoButton.place(relx=0.5, rely=0.35, anchor=CENTER)
timelapseButton = Button(self, text="Time-lapse Mode", font=helv18, bg="white", height=1, width=18)
timelapseButton.place(relx=0.5, rely=0.6, anchor=CENTER)
root = Tk()
helv18 = Font(family='Helvetica', size=18, weight='bold')
root.config(bg='black')
root.geometry("480x320")
root.resizable(0, 0)
app = Window(root)
root.mainloop()