Tkinter Bugs wrt adding mulitple buttons to menu bar - python

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

Related

Why do the buttons not appear in the tkinter window?

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)

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)

Adding a hovering option to my buttons in a class

I'm trying to add a hovering option for multiple buttons which I have already achieved, but I would like to do it in a class to save me adding the option to each button individually.
I'm coding in python and using tkinter for my GUI.
class GUIButtons():
def __init__(self, window):
self.window = window
self.Calculate = Button(window, command=GetUnits, text="Calculate", width = 19, background = "dark blue", fg="white")
self.Calculate.grid(row=1, column=4, sticky=NSEW)
self.ShowMethod = Button(window, command=ShowMethod, text="Show method", width = 19, background = "darkblue", fg="white")
self.ShowMethod.grid(row=1, column= 5, sticky=NSEW)
self.Submit = Button(window, command = lambda: GetCoordinate(Message), text="Submit", width = 6, height = 1, background = "dark blue", fg="white", font = 11)
self.Submit.grid(row=3, column = 3, sticky = NSEW)
self.Displacement = Button(window, text="Displacement", background = "Dark Blue", fg="white", font=11)
self.Displacement.grid(row=2, column=1, sticky= N)
Not sure how to bind the hover option just once for it to apply for all my buttons.
Any help would be highly appreciated!
See Instance and Class Bindings
But Tkinter also allows you to create bindings on the class and
application level; in fact, you can create bindings on four different
levels:
the widget class, using bind_class (this is used by Tkinter to provide
standard bindings)
and example
By the way, if you really want to change the behavior of all text
widgets in your application, here’s how to use the bind_class method:
top.bind_class("Text", "", lambda e: None)
So using bind_class with <Enter> and <Leave> you can do it.
--
EDIT: example - when mouse enter/hover any button then test() will be called.
from tkinter import *
# ---
def test(event):
print(event)
# ---
window = Tk()
# created befor binding
Button(window, text="Button #1").pack()
Button(window, text="Button #2").pack()
Button(window, text="Button #3").pack()
window.bind_class('Button', '<Enter>', test)
# created after binding
Button(window, text="Button #4").pack()
window.mainloop()
--
You can also create own Widget to change existing Widget.
Red button:
from tkinter import *
# ---
class RedButton(Button):
def __init__(self, parent, **options):
Button.__init__(self, parent, **options)
self['bg'] = 'red'
# or
#self.config(bg='red')
# ---
window = Tk()
RedButton(window, text="Button #1").pack()
RedButton(window, text="Button #2").pack()
RedButton(window, text="Button #3").pack()
window.mainloop()
or
class RedButton(Button):
def __init__(self, parent, **options):
Button.__init__(self, parent, bg='red', **options)
--
EDIT:
If you need to change only button colors on hover then you don't need to bind function. Button has activebackground= and activeforeground=.
import tkinter as tk
root = tk.Tk()
btn = tk.Button(root, text="HOVER", activebackground='blue', activeforeground='red')
btn.pack()
root.mainloop()
see Button
EDIT: it can behave different on Windows, Linux and OS X

Different Windows in TKinter

I am having trouble figuring out how to run more than one application in a Tkinter instance. I am planning to have one main window that will call other applications to run.
I also want to have the login button set the username and password strings, and then close its own window.
I have already tried using Toplevel windows, but this limits my ability to format buttons. For example I cant use frames... Any help on the direction I need to take is greatly appreciated.
I am spinning my wheels trying to figure this out, I have looked at many different code samples and I keep getting contradictory ways to do the same thing. I am using python 3.
import tkinter as tk
import os
import json
from tkinter import Toplevel
cert = ""
username = ""
password = ""
base_url = ''
url = ''
splash_page = ''
class BuildApplication(tk.Frame):
def __init__(self, master=None):
tk.Frame.__init__(self, master)
self.pack()
self.createWidgets()
def createWidgets(self):
self.lUSER = tk.Label(self, text="Username: ").pack(side="left")
self.eUSER = tk.Entry(self)
self.eUSER.pack(side="left")
self.lPASS = tk.Label(self, text="Password: ").pack(side="left")
self.ePASS = tk.Entry(self)
self.ePASS.pack(side="left")
#ive tried command= lambda: self.setCredentials(self.eUSER.get(),self.ePASS.get())
# command = self.setCredentials(self.eUSER.get(),self.ePASS.get())
# But none if it works....
self.LOGIN = tk.Button(self, text = "Login", fg="green" )
self.LOGIN.pack(side="left")
self.QUIT = tk.Button(self, text="QUIT", fg="red", command=self.destroy)
self.QUIT.pack(side="left")
self.mainloop()
def setCredentials(self,u,p):
username = u
password = p
print(username)
print(password)
self.destroy()
class SearchApplication(tk.Frame):
def __init__(self, master=None):
tk.Frame.__init__(self, master)
self.pack()
self.createWidgets()
def createWidgets(self):
print("create somethng")
class MainApplication(tk.Frame):
def __init__(self, master=None):
tk.Frame.__init__(self, master)
self.pack()
self.createWidgets()
def createWidgets(self):
#Build Data
self.bBuild = tk.Button(self, text="Build Data", command=self.build)
self.bBuild.pack(side="top")
#Search Data
self.bSearch = tk.Button(self, text="Search",command=self.search)
self.bSearch["text"] = "Search"
self.bSearch["command"] = self.search
self.bSearch.pack(side="top")
#quit
self.QUIT = tk.Button(self, text="QUIT", fg="red", command= root.destroy)
self.QUIT.pack(side="bottom")
def build(self):
print("Building")
root2 = tk.Tk()
buildApp = BuildApplication(master=root2)
buildApp.mainloop()
def search(self):
print("Search")
root3 = tk.Tk()
app2 = SearchApplication(master=root3)
app2.mainloop()
root = tk.Tk()
app = MainApplication(master=root)
app.master.title("fdsafds")
app.mainloop()
I modified your code:
import tkinter as tk
import os
import json
from tkinter import Toplevel
cert = ""
username = ""
password = ""
base_url = ''
url = ''
splash_page = ''
class BuildApplication(tk.Frame):
def __init__(self, master=None):
tk.Frame.__init__(self, master)
self.pack()
self.createWidgets()
def createWidgets(self):
self.lUSER = tk.Label(self, text="Username: ")
self.lUSER.pack(side="left")
self.eUSER = tk.Entry(self)
self.eUSER.pack(side="left")
self.lPASS = tk.Label(self, text="Password: ")
self.lPASS.pack(side="left")
self.ePASS = tk.Entry(self, show="*")
self.ePASS.pack(side="left")
#ive tried command= lambda: self.setCredentials(self.eUSER.get(),self.ePASS.get())
# command = self.setCredentials(self.eUSER.get(),self.ePASS.get())
# But none if it works....
self.LOGIN = tk.Button(self, text = "Login", fg="green", command=self.setCredentials )
self.LOGIN.pack(side="left")
self.QUIT = tk.Button(self, text="QUIT", fg="red", command=self.master.destroy)
self.QUIT.pack(side="left")
#self.mainloop()
def setCredentials(self):
username = self.eUSER.get()
password = self.ePASS.get()
print("username", username)
print("password", password)
self.master.destroy()
class SearchApplication(tk.Frame):
def __init__(self, master=None):
tk.Frame.__init__(self, master)
self.pack()
self.createWidgets()
def createWidgets(self):
print()
self.some_abel = tk.Label(self, text="create somethng")
self.some_abel.pack(side="left")
self.quitb = tk.Button(self, text = "quit", fg="green", command=self.master.destroy )
self.quitb.pack(side="left")
class MainApplication(tk.Frame):
def __init__(self, master=None):
tk.Frame.__init__(self, master)
self.pack()
self.createWidgets()
def createWidgets(self):
#Build Data
self.bBuild = tk.Button(self, text="Build Data", command=self.build)
self.bBuild.pack(side="top")
#Search Data
self.bSearch = tk.Button(self, text="Search",command=self.search)
self.bSearch["text"] = "Search"
self.bSearch["command"] = self.search
self.bSearch.pack(side="top")
#quit
self.QUIT = tk.Button(self, text="QUIT", fg="red", command= self.master.destroy)
self.QUIT.pack(side="bottom")
def build(self):
print("Building")
root2 = tk.Toplevel()
buildApp = BuildApplication(master=root2)
def search(self):
print("Search")
root3 = tk.Toplevel()
app2 = SearchApplication(master=root3)
root = tk.Tk()
app = MainApplication(master=root)
app.master.title("fdsafds")
app.mainloop()
The main change includes using toplevel windows in build and search methods instead of Tk() ones. You should not have multiple Tk().mainloops() in one program. Its better to use toplevel windows for new windows.
I also changed setCredentials method so that it actually works and prints out username and password and destroys its window upon pressing Login. Also I added few widgets to SearchApplication to show how to make it work.
Other problem was this line (and other lines similar to this one):
self.lUSER = tk.Label(self, text="Username: ").pack(side="left")
It does not make direct trouble in this particular code, but as a result of this line, self.lUSER is None. The reason is that pack (and grid) returns None. This might be not an issue now, but later it can save you lots of headache wondering why you cant change the label self.lUSER if you wanted to.
Below is gif showing how it works. Hope it helps.

Python tkinter: How can I ensure only ONE child window is created onclick and not a new window every time the button is clicked?

Currently learning tkinter and have come to a dead end.
Each time I click one of the buttons in my GUI (after typing 'username' and 'password' into the log in screen) a new child window is created and appears. As it should. What I would now like to do is to ensure that only one window is created and any subsequent clicks do not create yet more windows. How could this be done?
from tkinter import *
class Main():
def __init__(self, master):
self.master = master
self.master.title("Main Window")
self.button1 = Button(self.master, text="Click Me 1", command = self.Open1)
self.button1.grid(row=0, column=0, sticky=W)
self.button2 = Button(self.master, text="Click Me 2", command = self.Open2)
self.button2.grid(row=0, column=2, sticky=W)
self.button3 = Button(self.master, text="Close", command = self.Close)
self.button3.grid(row=1, column=0, sticky=W)
def Login(self):
login_win = Toplevel(self.master)
login_window = Login(login_win)
login_window.Focus()
#main_window.Hide()
def Open1(self):
second_window = Toplevel(self.master)
window2 = Second(second_window)
def Open2(self):
third_window = Toplevel(self.master)
window3 = Third(third_window)
def Close(self):
self.master.destroy()
#def Hide(self):
#self.master.withdraw()
#def Appear(self):
#self.master.deiconify()
class Second():
def __init__(self, master):
self.master = master
self.master.title("Child 1 of Main")
self.master.geometry("300x300")
self.button4 = Button(self.master, text="Click Me 1", command = self.Open_Child)
self.button4.grid(row=0, column=0, sticky=W)
def Open_Child(self):
second_child_window = Toplevel(self.master)
window4 = Second_Child(second_child_window)
class Third():
def __init__(self, master):
self.master = master
self.master.geometry("300x300")
self.master.title("Child 2 of Main")
class Second_Child():
def __init__(self, master):
self.master = master
self.master.geometry("400x300")
self.master.title("Child of 'Child 1 of Main'")
class Login():
def __init__(self, window):
self.window = window
self.window.title("Current Library")
Label(self.window, text="Log in to use this program:").grid(row=0, column=0, sticky=W)
self.userbox=Entry(self.window, width=20, bg="light green")
self.userbox.grid(row=1, column=0, sticky=W)
self.passbox=Entry(self.window, width=20, bg="light green")
self.passbox.grid(row=2, column=0, sticky=W)
Button(self.window, text="Submit", width=5, command=self.clicked).grid(row=3, column=0, sticky=W)
def Focus(self):
self.window.attributes('-topmost', 1)
self.window.grab_set()
def clicked(self):
username = self.userbox.get()
password = self.passbox.get()
if password == "password" and username == "username":
self.correct = True
self.window.grab_release()
self.window.destroy()
else:
pass
root_window = Tk()
root_window.iconbitmap(default='transparent.ico')
root_window.geometry("200x100")
main_window = Main(root_window)
main_window.Login()
root_window.mainloop()
Inside the functions which are called when the buttons are clicked could I add an IF statement?: IF window object does not exist then instantiate window object ELSE pass.
def Open1(self):
if window2 == False:
second_window = Toplevel(self.master)
window2 = Second(second_window)
else:
pass
If this is the correct logic then how can I check if a window object has already been created because the above doesn't work as I am referencing the object before it is created?
Many thanks.
Initialize the child window variable in Main's __init__.
self.child_window = None
Then you can check whether it exists in Open1.
def Open1(self):
if not self.child_window:
self.child_window = Second(Toplevel(self.master))
By the way, if you intend for Second to act like its own window, it's a bit awkward that you have to create a Toplevel every time you want to create a Second. Conventionally, you would make Second a subclass of Toplevel, so it can be created on its own. Something like:
class Second(Toplevel):
def __init__(self, master, *args, **kargs):
Toplevel.__init__(self, master, *args, **kargs)
#rest of initialization goes here.
#Use `self` everywhere you previously used `self.master`.
Now you could just do:
def Open1(self):
if not self.child_window:
self.child_window = Second(self.master)

Categories