I am trying to create buttons in a program to take the user to other screens. I have created the images on labels but not sure on how to make them into functioning buttons that take users to another page. You can see in my code the 2 buttons i need are the search quote and new quote. Thank you
from tkinter import *
class Window():
def __init__(self,master): #constructor
self.master = master
self.master.title("Borras Roofing Quotation System") #sets title of window
self.master.geometry("2160x1440") #sets size of window
self.master.configure(background = "white") # sets background colour of window.
self.Borras = PhotoImage(file = "Borras.Logo.PNG") #sets up image
self.BorrasLabel = Label(self.master, image = self.Borras, bg = "white", width='1000', height= '500') #puts image onto label
self.BorrasLabel.pack()
self.newquote = PhotoImage(file = "Process_New_Quote_Button.PNG") #sets up image
self.newquoteLabel = Label(self.master, image = self.newquote, bg = "white") #puts image onto label
self.newquoteLabel.place(x = 200, y = 500)
self.searchquote = PhotoImage(file = "Search_Current_Quote_Button.PNG") #sets up image
self.searchquoteLabel = Label(self.master, image = self.searchquote, bg = "white") #puts image onto label
self.searchquoteLabel.place(x = 800, y = 500)
root = Tk()
userPassWindow = Window(root)
root.mainloop()
You can use the inheritance and make each window child of the Toplevel widget.
Example:
from Tkinter import *
class search(Toplevel):
# Just inherits TopLevel window
def __init__(self, parent, master=None):
Toplevel.__init__(self, master)
#Save parent reference to modify parent view from search view
self.parent = parent
class quote(Toplevel):
def __init__(self, parent, master=None):
self.parent = parent
Toplevel.__init__(self, master)
class Window():
def __init__(self,master): #constructor
self.master = master
self.master.title("Borras Roofing Quotation System") #sets title of window
self.master.geometry("400x440") #sets size of window
self.master.configure(background = "white") # sets background colour of window.
self.Borras = PhotoImage(file = "Borras.Logo.PNG") #sets up image
self.BorrasLabel = Label(self.master, image = self.Borras, bg = "white") #puts image onto label
self.BorrasLabel.place(x = 10, y = 50)
self.newquote = PhotoImage(file = "Process_New_Quote_Button.PNG") #sets up image
self.newquoteLabel = Label(self.master, image = self.newquote, bg = "white") #puts image onto label
self.newquoteLabel.place(x = 150, y = 50)
self.searchquote = PhotoImage(file = "Search_Current_Quote_Button.PNG") #sets up image
self.searchquoteLabel = Label(self.master, image = self.searchquote, bg = "white") #puts image onto label
self.searchquoteLabel.place(x = 300, y = 50)
search_quote = Button(text="Search quote", command=self.search)
quote = Button(text="Quote", command=self.quote)
search_quote.pack()
quote.pack()
def search(self):
#Pass parent reference 'self'
search(self)
def quote(self):
quote(self)
root = Tk()
userPassWindow = Window(root)
root.mainloop()
For a start you need to use instances of Button, not Label if you want to achieve anything by clicking on it. If you add
command = anyfunction
as an argument to a button then the function will be run when the button is pressed (the function can be declared either inside or outside the Class, depending on what scope you want). From there use the
.pack_forget()
or
.place_forget()
procedure to remove items (though this does NOT delete them) and pack, place or grid the new items in your "window". If you are looking for actual screens to pack and forget pack then you should use tkinter Frames. Here is a useful link that will show you how to use each widget correctly:
http://effbot.org/tkinterbook
Related
I am trying to showcase tables of monday and tuesday however the canvas rectangles seem to keep a very large gap between them.Is it possible to not have any gaps at all or have a very minimal gap?
class SmokingPlan:
def __init__(self, master):
self.master = master
self.master.title('Kill Smoking')
self.master.geometry("1200x1200")
#Monday
canvas_monday = Canvas(self.master)
canvas_monday.grid(row = 0, column = 3)
canvas_monday.create_rectangle(10,10, 150, 200)
canvas_monday.create_text(60,25, text = "Monday")
#Tuesday
canvas_tuesday = Canvas(self.master)
canvas_tuesday.grid(row=0, column = 5)
canvas_tuesday.create_rectangle(10,10, 150, 200)
canvas_tuesday.create_text(60,25, text = "Tuesday")
You are drawing two Canvas widgets. The Canvas widget has a default size. By changing the color of your canvas you will see why you have a gap in between.
import tkinter as tk
class SmokingPlan:
def __init__(self, master):
self.master = master
self.master.title('Kill Smoking')
self.master.geometry("1200x1200")
#Monday
canvas_monday = tk.Canvas(self.master, bg="blue")
canvas_monday.grid(row = 0, column = 0)
canvas_monday.create_rectangle(10,10, 150, 200)
canvas_monday.create_text(60,25, text = "Monday")
#Tuesday
canvas_tuesday = tk.Canvas(self.master, bg="yellow")
canvas_tuesday.grid(row=0, column = 1)
canvas_tuesday.create_rectangle(10,10, 150, 200)
canvas_tuesday.create_text(60,25, text = "Tuesday")
if __name__ == '__main__':
root = tk.Tk()
app = SmokingPlan(root)
root.mainloop()
Now you could set smaller canvas size or even better put both rectangles in the same canvas and change the coords.
Set Canvas size example:
canvas_monday = tk.Canvas(self.master, bg="blue", width=200)
will shrink your monday canvas as follow:
I want to build an app where you can change the background image by clicking a button. First, to put a background to my frame, I created the image as a label and then sub-classed every other label to the background label. Now when you click the button "Change Background", it changes the background. However, all the labels disappear. Buttons only appear when you hover over them by the cursor. Labels never appear!
My questions are:
Why does this happen?
How to fix it?
Here is a simple code to reproduce the issue:
import tkinter as tk
from PIL import ImageTk, Image
class App():
def __init__(self, root):
self.root = root
self.controlFrame = tk.Frame(root, width=900, height=600)
self.controlFrame.pack_propagate(0) # Prevents resizing
self.controlFrame.pack()
img = Image.open('images/outside.jpg').resize((900, 600))
self.background_image = ImageTk.PhotoImage(img)
self.background_label = tk.Label(self.controlFrame, image=self.background_image)
self.background_label.configure(image = self.background_image)
self.background_label.pack(fill="both", expand="yes")
self.changeButton = tk.Button(self.background_label, text="Change Background",
command = self.changeBK)
self.changeButton.place(x=400, y=300)
self.someButton = tk.Button(self.background_label, text="Some Button")
self.someButton.place(x=400, y=100)
self.someOtherButton = tk.Button(self.background_label, text="Some Other Button")
self.someOtherButton.place(x=400, y=450)
self.userMessage = tk.Label(self.background_label, text="Label", height = 3 , width= 14, bg="white")
self.userMessage.place(x=400, y= 200)
def changeBK(self):
img = Image.open('images/muddyPath.jpg').resize((900, 600))
self.background_image = ImageTk.PhotoImage(img)
self.background_label.configure(image = self.background_image)
def main():
win = tk.Tk() # Create a window
win.title("Using Multiple Layouts") # Set window title
win.geometry("900x600") # Set window size
win.resizable(False, False) # Both x and y dimensions ...
# Create the GUI as a Frame
# and attach it to the window ...
myApp = App(win)
# Call the GUI mainloop ...
win.mainloop()
if __name__ == "__main__":
main()
So I am trying to make a text-based game, but also want to incorporate images into it, I have a main menu, a window that is always open, and then a game window, that should have the image in, but for some reason, it appears in the menu window instead. Has anyone got any idea why?
def menu():
master = Tk()
master.geometry("350x300")
master.wm_iconbitmap("zombie.ico")
master.configure(background = '#484a2c')
master.title("Menu")
def game():
master = Tk()
master.geometry("800x500")
master.title("Game")
master.wm_iconbitmap("zombie.ico")
master.configure(background = '#484a2c')
image = PhotoImage(file="Kitchenimage.gif")
label5 = Label(image=image)
label5.image = image
label5.pack()
label = Label(master, text = "Zombie Mansion", background = '#484a2c',
font = ("AR CHRISTY", 30))
label.pack()
b = Button(master,text = "Play Game", height = 1, width = 10)
b.config(background = '#484a2c', activebackground = '#484a2c', font =
("AR CHRISTY", 14), command = get_username)
b.place(x = 120, y = 70)
mainloop()
"Why is my image appearing on the wrong window in Tkinter?"
Assuming you have another instance of Tk as what you refer to as main menu somewhere in your code that you're not showing us like:
main = Tk()
in addition to master = Tk() in your game method, then it's because when you instantiate widgets without passing a parent widget explicitly like in:
label5 = Label(image=image)
then the widget's parent defaults to the Tk instance whose mainloop is being run, in above case supposedly main's. By default widgets are shown under their parents, hence the reason.
Pass parent explicitly like:
label5 = Label(master=main, image=image)
or
label5 = Label(master, image=image)
In most cases, if not all cases, you shouldn't have multiple instances of Tk. If you require additional windows, please use Toplevel widget.
I am trying to place two image buttons on my image background in a certain position, but my buttons are not appearing. I think their images are behind the background.
I tried to use place and pack, both did not work. What could be the problem?
from tkinter import*
import tkinter as tk
import settings
class Application(Frame):
def __init__ (self, master):
Frame.__init__(self,master)
self.grid()
self.create_widgets()
def create_widgets(self):
button1 = PhotoImage(file ="button1.gif")
button2 = PhotoImage(file ="button2.gif")
settings_button = Button(self, image = button1,
command = self.mult_command, width = 15)
settings_button.place(x=1, y=1)
rules_button = Button(self, image = button2,
command = self.the_rules, width = 15)
rules_button.place(x=50, y=50)
def main_code():
window = Tk()
window.title("The Bouncer")
bg_image = PhotoImage(file ="pic.gif")
x = Label (image = bg_image)
x.image = bg_image
x.place(x = 0, y = 0, relwidth=1, relheight=1)
window.geometry("600x300")
app = Application(window)
window.mainloop()
main_code()
thanks
It is likely that your image is being garbage collected before it is displayed. This is a common Tkinter gotcha. Try changing the lines:
button1 = PhotoImage(file ="button1.gif")
button2 = PhotoImage(file ="button2.gif")
to
self.button1 = PhotoImage(file ="button1.gif")
self.button2 = PhotoImage(file ="button2.gif")
and use
settings_button = Button(self, image = self.button1, command = self.mult_command, width = 15)
etc.
This should keep a reference to your image, stopping it from getting garbage collected.
In addition to keeping a reference to the image, you have a problem with this line:
self.grid()
in the __init__ method of Application. It's gridding the Frame into the window, but since nothing is ever packed or gridded into the frame, it doesn't ever expand past a little, tiny frame, so you just don't see the Buttons inside it. A simple fix here would be the pack method, with arguments to fill the window and expand when needed:
self.pack(fill=BOTH, expand=1)
I am trying to place two image buttons on my image background in a certain position, but my buttons are not appearing. I think their images are behind the background.
I tried to use place and pack, both did not work. What could be the problem?
from tkinter import*
import tkinter as tk
import settings
class Application(Frame):
def __init__ (self, master):
Frame.__init__(self,master)
self.grid()
self.create_widgets()
def create_widgets(self):
button1 = PhotoImage(file ="button1.gif")
button2 = PhotoImage(file ="button2.gif")
settings_button = Button(self, image = button1,
command = self.mult_command, width = 15)
settings_button.place(x=1, y=1)
rules_button = Button(self, image = button2,
command = self.the_rules, width = 15)
rules_button.place(x=50, y=50)
def main_code():
window = Tk()
window.title("The Bouncer")
bg_image = PhotoImage(file ="pic.gif")
x = Label (image = bg_image)
x.image = bg_image
x.place(x = 0, y = 0, relwidth=1, relheight=1)
window.geometry("600x300")
app = Application(window)
window.mainloop()
main_code()
thanks
It is likely that your image is being garbage collected before it is displayed. This is a common Tkinter gotcha. Try changing the lines:
button1 = PhotoImage(file ="button1.gif")
button2 = PhotoImage(file ="button2.gif")
to
self.button1 = PhotoImage(file ="button1.gif")
self.button2 = PhotoImage(file ="button2.gif")
and use
settings_button = Button(self, image = self.button1, command = self.mult_command, width = 15)
etc.
This should keep a reference to your image, stopping it from getting garbage collected.
In addition to keeping a reference to the image, you have a problem with this line:
self.grid()
in the __init__ method of Application. It's gridding the Frame into the window, but since nothing is ever packed or gridded into the frame, it doesn't ever expand past a little, tiny frame, so you just don't see the Buttons inside it. A simple fix here would be the pack method, with arguments to fill the window and expand when needed:
self.pack(fill=BOTH, expand=1)