Two windows appear on my tkinter app - python

I am quite new to python and tkinter and I was trying to open iamges to use them as icons on my program, so I found I have to use tk.Toplevel instead of tk.Tk. However, when I do this, the application opens two windows,the one where everything is programmed and a blank one.
Here is the full code in case anyone could help:
import tkinter as tk
from tkinter import ttk
from tkinter import PhotoImage
from PIL import Image, ImageTk
Large_Font = ("Times", 20)
class RobProject(tk.Toplevel):
def __init__(self):
tk.Toplevel.__init__(self)
tk.Tk.wm_title(self, "Com-Bot Control")
container = tk.Frame(self)
container.pack(side="top", fill="both", expand = True)
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)
self.frames = {}
for F in (MenuPage, PageOne, PageTwo, PageThree):
frame = F(container, self)
self.frames[F] = frame
frame.grid(row=0, column=0, sticky="NEWS")
self.show_frame(MenuPage)
def show_frame(self, cont):
frame = self.frames[cont]
frame.tkraise()
class MenuPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self,parent)
label = tk.Label(self, text = "Menú", font=Large_Font)
label.pack(pady=10, padx=10)
RCB = ttk.Button(self, text="Control Remoto",
command= lambda: controller.show_frame(PageOne))
RCB.pack()
EOB = ttk.Button(self, text="EvitaObstáculos",
command= lambda: controller.show_frame(PageTwo))
EOB.pack()
SLB = ttk.Button(self, text="Siguelíneas",
command= lambda: controller.show_frame(PageThree))
SLB.pack()
img = Image.open("play-button.png")
photo = ImageTk.PhotoImage(img)
Show_Im = tk.Button(self, image=photo)
Show_Im.image = photo
Show_Im.pack()
class PageOne(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
label = tk.Label(self, text = "Control Remoto", font=Large_Font)
label.pack(pady=10, padx=10)
Back_B1 = ttk.Button(self, text="Atrás",
command= lambda: controller.show_frame(MenuPage))
Back_B1.pack()
class PageTwo(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
label = tk.Label(self, text = "Evitaobstáculos", font=Large_Font)
label.pack(pady=10, padx=10)
Back_B2 = ttk.Button(self, text="Atrás",
command= lambda: controller.show_frame(MenuPage))
Back_B2.pack()
class PageThree(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
label = tk.Label(self, text = "Siguelíneas", font=Large_Font)
label.pack(pady=10, padx=10)
Back_B3 = ttk.Button(self, text="Atrás",
command= lambda: controller.show_frame(MenuPage))
Back_B3.pack()
app = RobProject()
app.mainloop()
Thank you in advance!

You do not need Topelevel() to use images.
Not sure where you got that info but it is not accurate.
Toplevel() is specifically to create new windows outside of the root window in tkinter.
What is happening is when you call tk.Toplevel() tkinter is creating an instance of Tk() in order to run Toplevel(). This is where the blank screen is coming from. The blank screen is actually the root window without anything in it.
To fix your problem Change:
class RobProject(tk.Toplevel):
def __init__(self):
tk.Toplevel.__init__(self)
To:
class RobProject(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)

Related

How can I refresh/reinitialize a class with the values gotten from another class?

So the question title pretty much sums it up. I apologise in advance if I have too much information as I don't know exactly where the problem arises but the issue is in the transition between class StartPage and class PageOne.I basically want to get the text from first_button (I will add more of these later) and grab data from a file which has a name matching to the button text. This data will then be added in the for-loop in PageOne. However, it seems like PageOne is at its initial state despite the function transition()
from tempSimulator import lasdata
import tkinter as tk
class SalladBar:
def __init__(self,master):
self.master=master
master.geometry("1300x700")
container = tk.Frame(self.master)
container.pack(side="top", fill="both", expand=True)
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)
self.frames = {}
for F in (startPage, PageOne):
frame = F(container, self)
self.frames[F] = frame
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame(startPage)
def update_frame(self, page):
frame = self.frames[page]
frame.update()
def show_frame(self, page):
frame = self.frames[page]
frame.tkraise()
class variables:
data1=[]
data2=[]
class startPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
f1 = tk.Frame(self, width="1300", height="200", bd=6, relief="raise")
f1.pack()
label = tk.Label(f1, text="xxxx")
label.pack(pady=10, padx=10)
#Add more buttons
first_button= tk.Button(f1, text="Source",
command=lambda: self.transition(controller,first_button.cget("text")))
first_button.pack()
def transition(self,controller,source):
data1,data2=lasdata(source)
variables.data1=data1
variables.data2=data2
controller.update_frame(PageOne) #I thought this would do the trick
controller.show_frame(PageOne)
class PageOne(tk.Frame):
print("PageOne initialised")
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
f1=tk.Frame(self, width="1300", height="200", bd=6, relief="raise")
f1.pack()
label = tk.Label(f1, text="Title2")
label.pack(pady=10, padx=10)
for d in variables.data1:
sld_label= tk.Label(f1, text= d.givename() + "\n" + d.givedet())
sld_label.pack()
button1 = tk.Button(self, text="Back to Home",
command=lambda: controller.show_frame(startPage))
button1.pack()
root = tk.Tk()
app = SalladBar(root)
root.mainloop()
You need to move the data adding code to a separate method if you want to call it later. Things that are in the __init__ method are executed when the instance is created, and never again.
class PageOne(tk.Frame):
print("PageOne initialised")
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.f1=tk.Frame(self, width="1300", height="200", bd=6, relief="raise")
self.f1.pack()
label = tk.Label(self.f1, text="Title2")
label.pack(pady=10, padx=10)
button1 = tk.Button(self, text="Back to Home",
command=lambda: controller.show_frame(startPage))
button1.pack()
def add_data(self):
for d in variables.data1:
sld_label= tk.Label(self.f1, text= d.givename() + "\n" + d.givedet())
sld_label.pack()
Once you have done that you can call that method from another class like this:
def transition(self,controller,source):
data1,data2=lasdata(source)
variables.data1=data1
variables.data2=data2
controller.frames[PageOne].add_data() # update the other page
controller.show_frame(PageOne)

How to swap between pages / windows in Tkinter

I have tried googling and everyone seems to have structured their code completely differently. I understand the base level tkinter, however I do not understand how people are using classes and def's to swap pages. How can I swap from my main window to my second one? (and not open the second one after the main is closed)
import tkinter as tk
main = tk.Tk()
main.title("Main Program")
firstlabel = tk.Label(main, text="This is a program!")
firstlabel.pack()
main.mainloop()
second = tk.Tk()
second.title("Second Program")
firstlabel = tk.Label(second, text="This is another program!")
firstlabel.pack()
second.mainloop()
EDIT: (solution)
import tkinter as tk
class Application(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
container = tk.Frame(self)
container.pack(side="top", fill="both", expand = True)
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)
self.frames = {}
for F in (StartPage, PageOne, PageTwo):
frame = F(container, self)
self.frames[F] = frame
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame(StartPage)
def show_frame(self, cont):
frame = self.frames[cont]
frame.tkraise()
class StartPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self,parent)
label = tk.Label(self, text="Start Page", font=("Consolas", 30))
label.pack(pady=10,padx=10)
button = tk.Button(self, text="Visit Page 1", command=lambda: controller.show_frame(PageOne))
button.pack()
class PageOne(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
label = tk.Label(self, text="Page One!!!", font=("Consolas", 30))
label.pack(pady=10,padx=10)
button1 = tk.Button(self, text="Back to Home", command=lambda: controller.show_frame(StartPage))
button1.pack()
app = Application()
app.mainloop()
One way do this is by clearing everything (every widget), with this function:
def clear(app):
# Delete everything else in app
widget_list = app.winfo_children()
for item in widget_list:
if item.winfo_children():
widget_list.extend(item.winfo_children())
for item in widget_list:
item.pack_forget()
And then puting in the new window you want to swap to (every window should hav its own frame to make it simpler).
Try this::::
tk.Toplevel(main)

Python tkinter code - frames with imported image buttons

Currently the program opens up an empty blank window when trying to import images as buttons. Without trying to import the image as buttons, the code works fine however. Can anyone help fix this code up or point me in the right direction?
from tkinter import*
root = Tk()
class app(Tk):
def __init__(self, *args, **kwargs):
Tk.__init__(self, *args, **kwargs)
container = Frame(self)
container.pack(side="top", fill="both", expand = True)
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)
self.frames = {}
for F in (StartPage, SignIn, SignUp):
frame = F(container, self)
self.frames[F] = frame
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame(StartPage)
def show_frame(self, cont):
frame = self.frames[cont]
frame.tkraise()
class StartPage(Frame):
def __init__(self, parent, controller):
Frame.__init__(self, parent)
label = Label(self, text="Start Page")
label.pack(pady=10,padx=10)
img1 = PhotoImage(file="signinbtn.png")
button = Button(self, image=img1,
command=lambda: controller.show_frame(SignIn))
button.pack()
img2 = PhotoImage(file="signupbtn.png")
button2 = Button(self, image=img2, text="Sign Up",
command=lambda: controller.show_frame(SignUp))
button2.pack()
class SignIn(Frame):
def __init__(self, parent, controller):
Frame.__init__(self, parent)
label = Label(self, text="Sign In")
label.pack(pady=10,padx=10)
button1 = Button(self, text="Back to Start Page",
command=lambda: controller.show_frame(StartPage))
button1.pack()
class SignUp(Frame):
def __init__(self, parent, controller):
Frame.__init__(self, parent)
label = Label(self, text="Sign Up")
label.pack(pady=10,padx=10)
button1 = Button(self, text="Back to Start Page",
command=lambda: controller.show_frame(StartPage))
button1.pack()
root.mainloop()
The empty window is created by the second code line:
root = Tk()
Apart from this line you have just class definitions. As you never instantiate any of the classes no code from any class is run.

my frame are shown side by side. I want to show just one on top and switch them through a button

I use tkinter and every frames only have 1 image, 1 label and 1 button(that switches from frame to frame) they are all shown together side by side. the Label is the only element that change as I click the Button. can anyone help me put just one of them on top?
this is my code
import tkinter as tk
from tkinter import ttk
from PIL import Image, ImageTk
LARGE_FONT = ("Verdana", 12)
class main(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
container = tk.Frame(self)
container.pack(side="top", fill="both", expand= True)
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)
self.frames = { }
for F in (StartPage, Pageone):
frame = F(parent= container, controller= self)
self.frames[F] = frame
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame(StartPage)
def show_frame(self, cont):
frame = self.frames[cont]
frame.tkraise()
class StartPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
Label = tk.Label(self, text= "that's the first frame", font = LARGE_FONT)
Label.pack(pady=10, padx=10)
Button= ttk.Button(text= "visit page 1", command= lambda: controller.show_frame(Pageone))
Button.pack()
image = Image.open(r'/Users/me/Documents/pythonfolder/kushina.jpg')
image.thumbnail((300,300), Image.ANTIALIAS)
photo = ImageTk.PhotoImage(image)
Label_image = tk.Label(image=photo)
Label_image.image = photo
Label_image.pack()
class Pageone(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
Label = tk.Label(self, text= "that's the second frame", font = LARGE_FONT)
Label.pack(pady=10, padx=10)
Button= ttk.Button(text= "back to home", command= lambda: controller.show_frame(StartPage))
Button.pack()
image = Image.open(r'/Users/me/Documents/pythonfolder/kushina3.jpg')
image.thumbnail((300,300), Image.ANTIALIAS)
photo = ImageTk.PhotoImage(image)
Label_image = tk.Label(image=photo)
Label_image.image = photo
Label_image.pack()
app = main()
app.mainloop()
Pass parent to your widget initiation, or by default they'll be placed inside the root window Tk instance. Replace:
Button= ttk.Button(text= "visit page 1", ...)
with:
Button = ttk.Button(self, text= "visit page 1", ...)

Python Tkinter, tk.Frame container widget error in method

I am writing a GUI code that opens frame using Tkinter. I referred various websites. Now while testing I am facing problem. For example:
In 1st frame, I select MainController button.
in 2nd frame press MC_CONFIG button.
in 3rd frame I set XML PATH then clicked MC SYSTEM.xml button
If I go to Back to Home button and follow the same procedure, MC_CONFIG button gets disabled (i.e I cannot go further).
If I comment(delete) this line(126)
tk.Frame.__init__(self)
in method def nacxml(self): of class MC_CONFIG, it is working perfectly.
The below one is just part of my main code bu facing problem here.
Please guide me.
import Tkinter as tk
import xml.dom.minidom
from Tkinter import *
import tkMessageBox
from array import *
import tkFileDialog
import os
LARGE_FONT= ("Verdana", 12)
class SeaofBTCapp(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
tk.Tk.wm_title(self, "Switch Installer window")
container = tk.Frame(self)
container.pack(side="top", fill="both", expand = True)
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)
self.frames = {}
#for F in (StartPage, PageOne, PageTwo):
for F in (StartPage, MainController,xmlpath,MC_CONFIG):
frame = F(container,self)
self.frames[F] = frame
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame(StartPage)
def show_frame(self, cont):
frame = self.frames[cont]
frame.tkraise()
class StartPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.modules_label = ['MAINCONTROLLER']
self.modules_function = [MainController]
self.modules_label_index = len(self.modules_label)
self.modules_function_index = len(self.modules_function)
print("self.modules_label_index = %s" %self.modules_label_index)
label = Label(self, text="SWITCH INSTALLER", font=LARGE_FONT)
label.pack(pady=10,padx=10)
#button = Button(self, text="Visit Page 1",
button3 = Button(self, text="SELECT",
command=lambda: controller.show_frame(MainController))
button3.pack()
label3 = Label(self, text="MainController", font = LARGE_FONT)
label3.place(x= 50, y=100+10)
button8 = Button(self, text="Quit", command=self.quit)
button8.pack()
class xmlpath(tk.Frame):
#xfilename="+++"
def __init__(self, parent, controller):
self.xfilename="srinivasan"
tk.Frame.__init__(self, parent)
label = Label(self, text="Page One!!!", font=LARGE_FONT)
label.pack(pady=10,padx=10)
button1 = Button(self, text="Back to Home",
command=lambda: controller.show_frame(StartPage))
button1.pack()
button1 = Button(self, text="XML PATH",
command=self.newxmlpath)
button1.pack()
def newxmlpath(self,*args):
# ObjNAC= NacHandler()
self.filename = tkFileDialog.askopenfilename()
print(self.filename)
#ObjNAC.temp_method(self,self.filename)
return self.filename
class MainController(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
label = Label(self, text="|--Frame1 MainController --|", font = LARGE_FONT)
label.pack(pady=10,padx=10)
mc_button1 = Button(self, text="MC_CONFIG", command = lambda: controller.show_frame(MC_CONFIG))
mc_button1.pack()
mc_button2 = Button(self, text="MENU HOME", command = lambda: controller.show_frame(StartPage))
mc_button2.pack()
self.pack (fill = BOTH, expand = 1)
class MC_CONFIG(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
print "Inside MC_CONFIG"
self.database = []
# set root as parent
self.parent = parent
label1 = Label(self, text="|------------Frame2--MainController---------------|", font=LARGE_FONT)
label1.pack(pady = 10,padx = 10)
label2 = Label(self, text="Edit SYSTEM.xml File", font=LARGE_FONT)
label2.pack(pady = 10,padx = 10)
button1 = Button(self, text="XML PATH",
command=self.newxmlpath)
button1.pack(pady = 10,padx = 10)
button2 = Button(self, text = "MC SYSTEM.xml", command = self.nacxml)
button2.pack(pady = 10,padx = 10)
button3 = Button(self, text="Back to Home",
command=lambda: controller.show_frame(StartPage))
button3.pack(pady = 10,padx = 10)
def newxmlpath(self, *args):
self.filename = tkFileDialog.askopenfilename()
print(self.filename)
return self.filename
def nacxml(self):
tk.Frame.__init__(self)
print "===Inside Nacxml1==="
app = SeaofBTCapp()
app.geometry ("640x480+300+300")
app.mainloop()
The problem is this:
def nacxml(self):
tk.Frame.__init__(self)
You should only ever call the superclass constructor from within the constructor of the subclass. Doing so anywhere else will certainly not do whatever you think it's doing.
Finally code is working as intended
1.Deleted tk.Frame.__init__(self) as indicated by Bryan Oakley
def nacxml(self):
tk.Frame.__init__(self)
2.Controller is not intialized in self.But it is present in def __init__(self, parent, controller):.Hence added
self.controller = controller in MC_CONFIG class
class MC_CONFIG(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
print "Inside MC_CONFIG"
So can be used in below method as self.controller.show_frame(StartPage)
def nacxml(self):
tk.Frame.__init__(self)
print "===Inside Nacxml1==="
# xml read,edit,widget code will be added here
self.controller.show_frame(StartPage)

Categories