How to create a new Tkinter window frame within a window? - python

So basically I am making a GUI with Tkinter. I also want to make a frame within the Tkinter window, and it should open when I click a button.
Here is my code so far:
from tkinter import *
import tkinter
screen = Tk()
screen.title("My GUI")
screen.geometry("600x600")
screen.configure(background="Gray")
button1 = Button(screen)
button.pack()
screen.mainloop
So how do I make a new window(frame) when I click the button?

You can create/toggle frame following the below logic
from tkinter import *
import tkinter
screen = Tk()
screen.title("My GUI")
screen.geometry("600x600")
screen.configure(background="Gray")
frame_enabled = False
def toggle_frame():
global frame_enabled
if not frame_enabled:
my_frame.pack(fill='both', expand=True)
else:
my_frame.pack_forget()
frame_enabled = not frame_enabled
button1 = Button(screen, text="Toggle frame", command=toggle_frame)
button1.pack()
my_frame = Frame(screen, bg="red")
screen.mainloop()

Related

Button not showing up while opening new page (python tkinter)

I want to use the listbox in tkinter in order to open a new window with a button, but when I'm pressing a message it only opens the window, without the button.
from tkinter import *
from playsound import playsound
import os
# create a window as root using Tk() function
root = Tk()
root.geometry("200x200")
# create a changecolor function
# to change background color of window
def new_win():
top2 = Toplevel(root)
top2.geometry('200x200')
top2.title("display Window")
# photo2 = PhotoImage(file=r"hearing.png")
button = Button(text='wtf')
def changecolor(event):
# get selected list box color item
new_win()
# create listbox
listbox = Listbox(root , font=('times 20 bold'), height=5,width=10)
# insert color items into listbox
listbox.insert(1, 'first')
listbox.insert(2, 'second')
listbox.insert(3, 'third')
listbox.insert(4, 'forth')
listbox.pack(pady=20)
# bind the click event with listbox and
# call changecolor() function
listbox.bind('<<ListboxSelect>>', changecolor)
root.mainloop()
Any suggestion would be very helpful.
You can change the line 16
button = Button(text='wtf')
to
button = Button(top2,text='wtf')
button.pack()

Tkinter how to use a button to hide current window and open new one

Im trying to use a tkinter button that when clicked opens another window and hides the current one with the button inside.
def game():
window = tk.Toplevel()
window.geometry("1280x720")
root = tk.Tk()
root.title('testgame')
root.resizable(False,False)
root.geometry("500x500")
pbutton = tk.Button(root, text='Play', width=25, command=game and root.withdraw).place(relx = 0.5,rely = 0.5, anchor = 'center')
root.mainloop()
You can try something like this:
import tkinter as tk
root = tk.Tk()
#In order to hide main window
root.withdraw()
tk.Label(root, text="Main Window").pack()
aWindow = tk.Toplevel(root)
def change_window():
#remove the other window entirely
aWindow.destroy()
#make root visible again
root.iconify()
root.deiconify()
tk.Button(aWindow, text="This is aWindow", command=change_window).pack()
root.mainloop()

Adding an image to a button in Tkinter

I am trying to add an image to a button, but I have got some issues when I try to execute the current code. All it shows is an image with no words. I can't even see the button either. Is there some way of fixing my current code?
from tkinter import *
import tkinter as tk
root = tk.Tk()
root.geometry("960x600")
canvas = Canvas(root, width=500, height=500)
canvas.pack()
imagetest = PhotoImage(file="giftest.gif")
canvas.create_image(250, 250, image=imagetest)
button_qwer = Button(root, text="asdfasdf", image=imagetest)
root.mainloop()
You need to pack (or grid) your button in the window, here is how you could do:
import tkinter as tk
from tkinter import PhotoImage
def print_hello():
print('hello')
root = tk.Tk()
root.geometry("960x600")
imagetest = PhotoImage(file="giftest.gif")
button_qwer = tk.Button(root, text="asdfasdf", image=imagetest, command=print_hello)
button_qwer.pack() # <-- don't forget to place the button in the window
root.mainloop()
You can have both text and image displayed on your button, using the compound option, like this:
button_qwer = tk.Button(root, image=imagetest, text="asdfasdf", compound="top", command=print_hello)
compound options are bottom, center, left, none, right, or top
You are making the button successfully but you are not drawing it onto the screen/interface. Use pack , place or grid.
button_qwer = Button(root, text="asdfasdf", image=imagetest)
button_qwer.pack()
Your full code can be like:
from tkinter import *
import tkinter as tk
root = tk.Tk()
root.geometry("960x600")
canvas = Canvas(root, width=500, height=500)
canvas.pack()
imagetest = PhotoImage(file="giftest.gif")
canvas.create_image(250, 250, image=imagetest)
button_qwer = Button(root, text="asdfasdf", image=imagetest)
button_qwer.pack()
root.mainloop()

New windows in tkinter

I have a bit of difficulty with the code below. Basically, I want the code to, when I press the Enter button, to open the window2 but also close window1 simultaneously so that there is only one window and not two of them.
The code is...
from tkinter import *
def window1():
window = Tk()
window.title("Welcome")
f = Frame()
f.pack()
label1 = Label(window, text = "Welcome to the random window")
label1.pack()
button1 = Button(window, text = "Enter...", command = window2)
button1.pack()
def window2():
screen = Tk()
screen.title("Pop-Up!")
fr = Frame()
fr.pack()
label2 = Label(screen, text = "This is a pop-up screen!")
label2.pack()
button2 = Button(screen, text = "Return", command = window1)
button2.pack()
window1()
This is "Bad" because you're using two instances of Tk. Try instead using TopLevels.
import tkinter as tk
def window1():
window = tk.Toplevel(root)
window.title("Welcome")
# etc etc ...
tk.Button(window,text="Enter...",command=lambda: window2(window)).pack()
def window2(old_window):
old_window.destroy()
# window2 stuff
root = tk.Tk()
root.iconify() # to minimize it, since we're just using Toplevels on top of it
window1()
root.mainloop()
When you are using the Tk() function, you are creating a new instance of the Tcl/tkinter interpreter. Instead use Toplevel() which will make a new window in the current interpreter.

make a button open only one window at a time (enable a button by closing a Toplevel window)

I want NewWinButton to create only one new window at a time, which means if
if NewWin.winfo_exists() == 1:
NewWinButton.config(state='disabled')
else:
NewWinButton.config(state='normal')
I can make this work if I add a button to the new window (QuitButton in this example):
import tkinter as tk
root = tk.Tk()
root.title('Main Window')
root.geometry('400x400')
def get_new_win():
NewWin = tk.Toplevel(root)
NewWin.title('New Window')
NewWin.geometry('300x300')
NewWinButton.config(state='disable')
def quit_win():
NewWin.destroy()
NewWinButton.config(state='normal')
QuitButton = tk.Button(NewWin,text='Quit', command=quit_win).pack()
NewWinButton = tk.Button(root,text='New Window', get_new_win).pack()
root.mainloop()
This works if and only if I use QuitButton to close the new window; however, if I use the close button in the new window, then the NewWinButton will remain 'disabled'.
Can anyone tell me how to fix this?
Use NewWin.protocol("WM_DELETE_WINDOW", quit_win) to assign function quit_win to the close button.
import tkinter as tk
root = tk.Tk()
root.title('Main Window')
root.geometry('400x400')
def get_new_win():
NewWin = tk.Toplevel(root)
NewWin.title('New Window')
NewWin.geometry('300x300')
NewWinButton.config(state='disable')
def quit_win():
NewWin.destroy()
NewWinButton.config(state='normal')
QuitButton = tk.Button(NewWin, text='Quit', command=quit_win)
QuitButton.pack()
NewWin.protocol("WM_DELETE_WINDOW", quit_win)
NewWinButton = tk.Button(root, text='New Window', command=get_new_win)
NewWinButton.pack()
root.mainloop()
BTW:
The pack() method returns None, not a button instance:
NewWinButton = tk.Button(...).pack()
use this:
NewWinButton = tk.Button(...)
NewWinButton.pack()

Categories

Resources