Python Tkinter panel

In this tutorial, we will learn How to create a panel in Python Tkinter and we will also cover different examples related to Tkinter Panel. And we will also cover these topics.

  • Python Tkinter panel
  • Python Tkinter add a panel
  • Python Tkinter button panel
  • Python Tkinter scroll panel

Python Tkinter panel

In this section, we will learn how to create a panel in Python Tkinter.

The panel behaves like a container, it contains several panes as a child widget and is arranged vertically or horizontally. The child widget can be resized by the user and every pane contain one widget.

Syntax:

p1 = PanedWindow(master,options)

master is used as a parent widget.

Code:

In the following code, we see a paned window is created with orient= vertical. And inside the paned window, we have created some labels.

The orient is set to vertical if the user places the child widget top to bottom. And if the orient is set to Horizontal then, the child widget is placed side to side.

from tkinter import *

ws=Tk()
ws.title("Python Guides")
ws.geometry("500x300")

p1 = PanedWindow()
p1.pack(fill=BOTH, expand=1)

left = Label(p1, text="Left Panel")
p1.add(left)

p2 = PanedWindow(p1, orient=VERTICAL)
p1.add(p2)

top = Label(p2, text="Top Panel")
p2.add(top)

bottom = Label(p2, text="Bottom Panel")
p2.add(bottom)

ws.mainloop()

Output:

After running the above code, we get the following output where we can see a paned window is created inside the window there is a child window. As we set PanedWindow(p1, orient=VERTICAL), we want to place the child window from top to bottom.

Python Tkinter Panel
Python Tkinter Panel Output

Read: Python Tkinter Entry

Python Tkinter add a panel

In this section, we will learn how to add a panel in Python Tkinter.

READ:  Python Tkinter Quiz - Complete tutorial

As we know, a panel window is a container we can add anything inside it like buttons, labels, entry widgets. And we can also add panels to the main window by PanedWindow(master, options).

Code:

In the following code, we add a panel inside the panel we even added a button with the text “Heya Click Me !\n I am a button”. And also added a check button with the text “Choose Me !”.

  • pawin.add(top) is used to add a button in the panel.
  • pawin.add(button) is used to add a check button in the panel.
from tkinter import *

ws = Tk()
ws.title("Python Guides")
ws.geometry("500x300")


pawin = PanedWindow(orient ='vertical')


top = Button(pawin, text ="Heya Click Me !\n I am  a button")
top.pack(side = TOP)

pawin.add(top)


button = Checkbutton(pawin, text ="Choose Me !")
button.pack(side = TOP)

pawin.add(button)

pawin.pack(fill = BOTH, expand = True)

pawin.configure(relief = RAISED)

ws.mainloop()

Output:

After running the above code, we get the following output in which we divide a paned window into two panes. One is the button pane and the other is the checkbox pane.

Python tkinter add panel
Python Tkinter add panel Output

Read: Python Tkinter Radiobutton

Python Tkinter button panel

In this section, we will learn how to create a button panel in Python Tkinter.

We can create a button pannel inside the panel window which helps us to execute any task with a single click.

Code:

In the following code, we create a panel window with orient =vertical inside this panel window, we create two-panel buttons. One is with the label “command” and another is with the label “Exit“. As the name suggests for which purpose the second button is used.

from tkinter import *
    

def write_a_slogan():
    print("Life is short\Do what you Love.!!")

ws = Tk()
ws.title("Python Guides")
ws.geometry("200x200")
pawin = PanedWindow(orient ='vertical')
frame = tk.Frame(ws)
frame.pack()

but = Button(frame, 
                   text="Exit", 
                   fg="blue",
                   command=quit)
but.pack(side=LEFT)
slog = Button(frame,
                   text="command",
                   fg="red",
                   command=write_a_slogan)
slog.pack(side=LEFT)

ws.mainloop()

Output:

READ:  Python Turtle Oval

In the following output, we see two buttons panel one is the command button and the other is the Exit button.

Python Tkinter button panel
Python Tkinter button panel Output

On clicking on the command button, we get the text shown on the command prompt. If we click on the Exit button, the whole program will be terminated.

Python Tkinter button panel1
Python Tkinter button panel1 Output

Read: Python Tkinter Button

Python Tkinter scroll panel

In this section, we will learn about how we can create a scroll panel in Python Tkinter.

Scroll Panel is used for scrolling the panel window in a predefined direction (Horizontal or vertical). A scroll panel is used when the content exceeds the panel window and the user wants to read the full content of the panel window.

Code:

In this code, we create a panel window in which content is written. The content exceeds the panel window and the user won’t read the whole content for reading the whole content we create a scroll panel.

Orient determines whether the scrollbar will be Vertical or Horizontal.



from tkinter import *

class ScrollBar:
	
	
	def __init__(self):
		
		
		ws = Tk()
		ws.title("Python Guides")
		ws.geometry("200x200")
		
		pw = PanedWindow(orient ='vertical')

		
		h1 = Scrollbar(ws, orient = 'horizontal')

		
		h1.pack(side = BOTTOM, fill = X)

		
		v1 = Scrollbar(ws)

		
		v1.pack(side = RIGHT, fill = Y)
		

		
		t1 = Text(ws, width = 15, height = 15, wrap = NONE,
				xscrollcommand = h1.set,
				yscrollcommand = v1.set)

		
		for i in range(20):
			t1.insert(END,"Python Guides Tutorial..!!\n")

		t1.pack(side=TOP, fill=X)

		
		h1.config(command=t1.xview)

		
		v1.config(command=t1.yview)

		
		ws.mainloop()


s1 = ScrollBar()

Output:

In the following output, we see there is only a vertical scroll panel when we want to read the whole content from top to bottom. Then we scroll the vertical scroll panel.

Python Tkinter scrollbar panel
Python Tkinter scrollbar panel Output

You may also like to read the following topics on Python Tkinter.

READ:  Matplotlib savefig blank image

So, in this tutorial, we discuss Python Tkinter Panel and we have also covered different examples related to its implementation. Here is the list of examples that we have covered.

  • Python Tkinter panel
  • Python Tkinter add a panel
  • Python Tkinter button panel
  • Python Tkinter scroll panel