How to work with multiple windows in Tkinter - python-3.x

I want to have 2 tkinter Windows. On the main Window there should be a button which opens a new window and closes the original one. Below you can see my minimal Code example.
So far the first window opens but as soon as I push the button both windows just close.
Please tell me what my specific error is int this code?
Is there a way to emprove my code?(general tips)?
.
def Func_Show_Rep(self):
#destroy main window
Cockpit_Win.quit()
Cockpit_Win.destroy()
# Open new Window
ReportSelection_Win = Toplevel()
ReportSelection_Win = Tk()
#create the main window
Cockpit_Win = Tk()
Btt_Show_Rep = Button(Cockpit_Win, text="Reports", width=35)
Btt_Show_Rep.bind("<Button-1>",Func_Show_Rep)#Button click starts function
Btt_Show_Rep.grid(column=1, row=2, padx=10, pady=7, sticky=E)
#Make the windows stay (loop)
Cockpit_Win.mainloop()

You cannot have a TopLevel window when the root window is destroyed with Tkinter. The TopLevel window is built on top of the root window.
If you want to destroy (effectively hide) the Cockpit_Win window, use the following:
Cockpit_Win.withdraw()
When you want to make it visible again, use:
Cockpit_Win.deiconify()
The entire code looks like this:
def Func_Show_Rep(self):
# Open new Window
ReportSelection_Win = Toplevel()
#ReportSelection_Win = Tk() # This is the same as the previous statement
#destroy (effectively hide) main window
Cockpit_Win.withdraw()
#create the main window
Cockpit_Win = Tk()
Btt_Show_Rep = Button(Cockpit_Win, text="Reports", width=35)
Btt_Show_Rep.bind("<Button-1>",Func_Show_Rep)#Button click starts function
Btt_Show_Rep.grid(column=1, row=2, padx=10, pady=7, sticky=E)
#Make the windows stay (loop)
Cockpit_Win.mainloop()

Related

CLOSED new Tk window opens automatically

Hi I had this exact same question :
opening a new window with a button
But as I ran the code given : (EDIT : first line is indeed from tkinter import *)
from tkinter import *
def create_window():
window = Toplevel(root)
root = Tk()
b = Button(root, text="Create new window", command=create_window)
b.pack()
root.mainloop()
the new window opens automatically, without waiting for the button to be clicked
why is that?

Can't put buttons on second window using Tkinter

I don't know why is the window created with nothing when I've put 3 buttons to appear. This is the function where it happens:
def Click():
if input_fieldContra.get() == contraseƱa:
vent_iniciada = Tk()
root.withdraw()
vent_iniciada.geometry("250x200")
vent_iniciada.mainloop()
nuevoB = Button(vent_iniciada, text="Nuevo", command=NuevoBot)
abrirB = Button(vent_iniciada, text="Abrir", command=AbrirBot)
guardarB = Button(vent_iniciada, text="Guardar", command=GuardarBot)
nuevoB.grid(vent_iniciada, row=0, column=0)
abrirB.grid(vent_iniciada, row=2, column=0)
guardarB.grid(vent_iniciada, row=3, column=0)
Firstly, .grid() doesn't take on the parent window. When using grid you should define the parent window inside the widget when widget is created.
.grid() options must be -column, -columnspan, -in, -ipadx, sticky etc.
Secondly, if you move vent_iniciada.mainloop() to the bottom of the code, it should work. What happens is that the code is executing:
vent_iniciada = TK()
root.withdraw
vent_iniciada.geometry("250x200")
and this is where you should put your button so the code reads them into the window.
Finally, after gridding the widgets, you tell the program to mainloop everything
vent_iniciada.mainloop()
You have to move :
vent_iniciada.mainloop()
to the last line of the function

How to put an object in a specefic row or column while using pack in tkinter

I have been making some gui applications using python tkinter.In tkinter pack and grid cannot be used together.While writing a code I had to use pack and then I needed to use 2 features called 'Cloumn' and 'row' of grid but that is impossible.
from tkinter import *
root = Tk()
button = Button(root, text="Click Me")
button.pack(side="bottom") #But I want to put that button in row=3
root.mainloop()
I need to put that button in row number 3.But how can I do it?
Is there any way to do so?
In order for pack and grid to be used in the same code you need to make sure you use them on separate containers. Each container (root window, toplevel window, frame) can only have either grid() or pack().
For example if I need to use pack on a frame and then use grid inside of that frame that is ok. But I cannot use pack inside the frame and also grid inside the frame.
If you expand the window made from the below code you will see how pack and grid can work together if used properly.
import tkinter as tk
root = tk.Tk()
top_frame = tk.Frame(root)
top_frame.pack(sid="top")
bot_frame = tk.Frame(root)
bot_frame.pack(sid="bottom")
tk.Label(top_frame, text="Row 0 of top_frame").grid(row=0, column=0)
tk.Label(bot_frame, text="Row 0 of bot_frame").grid(row=0, column=0)
tk.Label(bot_frame, text="Row 1 of bot_frame").grid(row=1, column=0)
tk.Label(bot_frame, text="Row 2 of bot_frame").grid(row=2, column=0)
tk.Button(bot_frame, text="Row 3 of bot_frame").grid(row=3, column=0)
root.mainloop()

okay so I created this code to create a GUI but i need to add buttons and when ever i try it creates a new window. what should I do?

this is my code so far o cant add buttons with out it creating more windows
////////
#import tkinter
import tkinter
#import tkmessagebox(buttons)
from tkinter import *
#create a new window
window = tkinter.Tk()
#title <------ put it before .mainloop
window.title("yeahh boiiii")
#window size
window.geometry("500x500")
#set a window icon
window.iconbitmap('N:\downloads\icon.ico.ico')#<---- 8bit file name
master = Tk()
def callback():
print ("click!")
b = Button(master, text="OK", command=callback)
b.pack()
#draws the window
window.mainloop()
////////
please help
Your problem is that you create 2 instances of Tk(). This is a bad idea, and you don't need to do it since you can make your button a child of the window object:
# Import tkinter
import tkinter as tk
# Create a new window
window = tk.Tk()
# Title <------ put it before .mainloop
window.title("yeahh boiiii")
# Window size
window.geometry("500x500")
# Set a window icon
window.iconbitmap('N:\downloads\icon.ico.ico') #<---- 8bit file name
def callback():
print ("click!")
b = tk.Button(window, text="OK", command=callback)
b.pack()
# Draw the window
window.mainloop()
I also rewrote your tkinter import, because you were importing it twice...

Raise Frame on top in PanedWindow in tkinter python 3.5

I have a frame in PanedWindow which i need on every tkinter GUI (in this case it's topFrame). Below it are many frames and I want to switch between those frame on button click (just like in any software where the top portion of screen is fixed and clicking on buttons the lower portion of GUI changes).
I know i need grid layout for it. But, it is not happening and i am not getting a solution anywhere.I have researched a lot on this topic everywhere but this solution is nowhere. Here is my code... i have written in comments those code which i feel are not working fine.
#python 3.5
from tkinter import *
#function to raise the frame on button click
def raiseFrame(frame):
frame.tkraise()
m = PanedWindow(height=500, width=1000, orient=VERTICAL)
m.pack(fill=BOTH, expand=1)
#to expand the column and row to fill the extra space
m.grid_columnconfigure(index=0, weight=1) #this code is not working as it should
m.grid_rowconfigure(index=0, weight=1) #this code is not working as it should
#top frame has two buttons which switches the bottom frames
topFrame = Frame(m, bg="blue")
m.add(topFrame)
button1 = Button(topFrame, text="Raise Frame 2", command=lambda: raiseFrame(frame2)) #raises frame 2 on clicking it
button1.pack(side=LEFT)
button2 = Button(topFrame, text="Raise Frame 1", command=lambda: raiseFrame(frame1)) #raises frame 1 on clicking it
button2.pack(side=LEFT)
#bottomframe acts as container for two other frames which i need to switch
bottomframe = Frame(m, bg="orange")
m.add(bottomframe)
frame1 = Frame(bottomframe, bg="yellow")
frame1.grid(row=0, column=0, sticky="news") ## sticky is not working ##
frame2 = Frame(bottomframe, bg="green")
frame2.grid(row=0, column=0, sticky="news") ## sticky is not working ##
label1 = Label(frame1, text="i should change")
label1.pack(padx=10, pady=10)
label2 = Label(frame2, text="i am changed !!")
label2.pack(padx=10, pady=10)
mainloop()
1)Please correct my code.
2)Explain me why in the "topFrame" even though i have not written
topFrame.pack(fill=BOTH, expand=True)
my code is showing the above property and it's expanding as well as filling both X and Y.
Same goes for the bottomFrame, it's orange colour is filling the entire space which does not happen in normal frames. So, is it some special feature of PanedWindow ?
You don't want to call topFrame.pack() and m.add(topFrame). You either pack/place/grid the window, or you add it to a paned window, you don't do both.
Also, if the frame is going to be in the paned window it needs to be a child of the paned window rather than a child of the root window.

Resources