Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
Python Tkinter - Create Button Widget
Next article icon

Tkinter is a GUI toolkit used in python to make user-friendly GUIs.Tkinter is the most commonly used and the most basic GUI framework available in Python. Tkinter uses an object-oriented approach to make GUIs.

Note: For more information, refer toPython GUI – tkinter

Toplevel widget

A Toplevel widget is used to create a window on top of all other windows. The Toplevel widget is used to provide some extra information to the user and also when our program deals with more than one application. These windows are directly organized and managed by the Window Manager and do not need to have any parent window associated with them every time.

Syntax:  

toplevel = Toplevel(root, bg, fg, bd, height, width, font, ..)

Optional parameters  

  • root = root window(optional) 
  • bg = background colour 
  • fg = foreground colour 
  • bd = border 
  • height = height of the widget. 
  • width = width of the widget. 
  • font = Font type of the text. 
  • cursor = cursor that appears on the widget which can be an arrow, a dot etc. 

Common methods  

  • iconify turns the windows into icon. 
  • deiconify turns back the icon into window. 
  • state returns the current state of window. 
  • withdraw removes the window from the screen. 
  • title defines title for window. 
  • frame returns a window identifier which is system specific. 

Example 1:  

Python3
fromtkinterimport*root=Tk()root.geometry("200x300")root.title("main")l=Label(root,text="This is root window")top=Toplevel()top.geometry("180x100")top.title("toplevel")l2=Label(top,text="This is toplevel window")l.pack()l2.pack()top.mainloop()

Output

python-tkinter-toplevel


Example 2:Creating Multiple toplevels over one another 

Python3
fromtkinterimport*# Create the root window# with specified size and titleroot=Tk()root.title("Root Window")root.geometry("450x300")# Create label for root windowlabel1=Label(root,text="This is the root window")# define a function for 2nd toplevel# window which is not associated with# any parent windowdefopen_Toplevel2():# Create widgettop2=Toplevel()# define title for windowtop2.title("Toplevel2")# specify sizetop2.geometry("200x100")# Create labellabel=Label(top2,text="This is a Toplevel2 window")# Create exit button.button=Button(top2,text="Exit",command=top2.destroy)label.pack()button.pack()# Display until closed manually.top2.mainloop()# define a function for 1st toplevel# which is associated with root window.defopen_Toplevel1():# Create widgettop1=Toplevel(root)# Define title for windowtop1.title("Toplevel1")# specify sizetop1.geometry("200x200")# Create labellabel=Label(top1,text="This is a Toplevel1 window")# Create Exit buttonbutton1=Button(top1,text="Exit",command=top1.destroy)# create button to open toplevel2button2=Button(top1,text="open toplevel2",command=open_Toplevel2)label.pack()button2.pack()button1.pack()# Display until closed manuallytop1.mainloop()# Create button to open toplevel1button=Button(root,text="open toplevel1",command=open_Toplevel1)label1.pack()# position the buttonbutton.place(x=155,y=50)# Display until closed manuallyroot.mainloop()

Output 

python-tkinter-toplevel


 


Improve

Similar Reads

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood ourCookie Policy &Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp