Home > Software engineering >  Adding code into another window in tkinter
Adding code into another window in tkinter

Time:11-01

I have managed to create a new GUI (zoom_menu), but when I open it, it does not have any code. How do I insert all of the code below into the new GUI?

from tkinter import *
root =Tk()
root.title("Carbon Emission Calculator")
root.geometry("2560x1600")




def zoom_menu():
   top = Toplevel()
   top.title("Zoom Menu")
   
btn = Button(root, text = "Zoom", command = zoom_menu).pack()

#defining selections for radio buttons
x = IntVar()
y = IntVar()


#type of call
type_of_call = Label(root, text="Number of participants", font="Arial 20").pack(pady = 10)
Radiobutton(root,text= "1:1 call", variable= x, value=1).pack()
Radiobutton(root,text= "Group call", variable= x, value=2).pack()


#call quality
call_quality = Label(root, text="Call Quality", font="Arial 20").pack(pady = (40,10))
Radiobutton(root,text= "Default", variable= y, value=1).pack()
Radiobutton(root,text= "720p HD", variable= y, value=2).pack()
Radiobutton(root,text= "1080p HD", variable= y, value=3).pack()

#main title
main_title = Label(root, text="Carbon Emissions from Zoom Calls", font="Arial 25").pack(pady=75, padx=0)

root.mainloop()

CodePudding user response:

You can use the variable top as a replacement for the root variable, i.e the top variable works same as how the root variable works.

For example:

from tkinter import *

root = Tk()
root.title("Carbon Emission Calculator")
root.geometry("2560x1600")

def zoom_menu():
    top = Toplevel()
    top.title("Zoom Menu")
    btn = Button(top, text = "Zoom", command = zoom_menu).pack()

    x = IntVar()
    y = IntVar()

    #type of call
    type_of_call = Label(root, text="Number of participants", font="Arial 20").pack(pady = 10)
    Radiobutton(top,text= "1:1 call", variable= x, value=1).pack()
    Radiobutton(top,text= "Group call", variable= x, value=2).pack()

    #call quality
    call_quality = Label(top, text="Call Quality", font="Arial 20").pack(pady = (40,10))
    Radiobutton(top,text= "Default", variable= y, value=1).pack()
    Radiobutton(top,text= "720p HD", variable= y, value=2).pack()
    Radiobutton(top,text= "1080p HD", variable= y, value=3).pack()

    #main title
    main_title = Label(top, text="Carbon Emissions from Zoom Calls", font="Arial 25").pack(pady=75, padx=0)

    top.mainloop()

   
btn = Button(root, text = "Zoom", command = zoom_menu).pack()

#defining selections for radio buttons
x = IntVar()
y = IntVar()

#type of call
type_of_call = Label(root, text="Number of participants", font="Arial 20").pack(pady = 10)
Radiobutton(root,text= "1:1 call", variable= x, value=1).pack()
Radiobutton(root,text= "Group call", variable= x, value=2).pack()

#call quality
call_quality = Label(root, text="Call Quality", font="Arial 20").pack(pady = (40,10))
Radiobutton(root,text= "Default", variable= y, value=1).pack()
Radiobutton(root,text= "720p HD", variable= y, value=2).pack()
Radiobutton(root,text= "1080p HD", variable= y, value=3).pack()

#main title
main_title = Label(root, text="Carbon Emissions from Zoom Calls", font="Arial 25").pack(pady=75, padx=0)

root.mainloop()


CodePudding user response:

import tkinter as tk #dont use wildcard import to avoid name conflicts

def zoom_menu(): #function after imports following PEP8 
   top = tk.Toplevel()
   myframe2 = MyFrame(top)
   myframe2.pack()
   top.title("Zoom Menu")

root =tk.Tk()
root.title("Carbon Emission Calculator")
root.geometry("2560x1600")
   
btn = tk.Button(root, text = "Zoom", command = zoom_menu)#construction method returns a reference
btn.pack()#geometry method returns None
#seperate them to keep a reference

#Using a class to have a "blueprint" that you can reuse
class MyFrame(tk.Frame):
    def __init__(self,master):
        super().__init__(master)#using super
        self.x = tk.IntVar() #self. represents the instance/copy of that object which you are using
        self.y = tk.IntVar()

        self.type_of_call = tk.Label(self, text="Number of participants", font="Arial 20") 

        self.r1 = tk.Radiobutton(self,text= "1:1 call", variable= self.x, value=1) #keep a reference for access
        self.r2 = tk.Radiobutton(self,text= "Group call", variable= self.x, value=2)

        self.call_quality = tk.Label(self, text="Call Quality", font="Arial 20")

        self.r3 = tk.Radiobutton(self,text= "Default", variable= self.y, value=1)
        self.r4 = tk.Radiobutton(self,text= "720p HD", variable= self.y, value=2)
        self.r5 = tk.Radiobutton(self,text= "1080p HD", variable= self.y, value=3)

        self.main_title = tk.Label(self, text="Carbon Emissions from Zoom Calls", font="Arial 25")

        #first construct all widgets and have your geometry in the same place
        #espacally with pack the order of packing them matters
        self.type_of_call.pack(pady = 10) 
        self.r1.pack()
        self.r2.pack()
        self.call_quality.pack(pady = (40,10))
        self.r3.pack()
        self.r4.pack()
        self.r5.pack()
        self.main_title.pack(pady=75, padx=0)
        
        
myframe = MyFrame(root)
myframe.pack()

root.mainloop()
  • Related