1

I'm trying to implement a resizable window with some widgets inside it, some of which should be centered and some left-justified. Currently, all widgets are centered:

from tkinter import *

root = Tk()

root.resizable()
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
x_cordinate = int((screen_width / 2) - (300 / 2))
y_cordinate = int((screen_height / 2) - (200 / 2))
root.geometry("{}x{}+{}+{}".format(300, 200, x_cordinate, y_cordinate))
root.title("Pane Sticky Test")

main_pane = PanedWindow(root, orient=VERTICAL)
main_pane.pack(fill=BOTH, expand=1)

var = IntVar()

# check = Checkbutton(main_pane, text="My option", variable=var, sticky=W) --> error unknown option "-sticky"
check = Checkbutton(main_pane, text="My option", variable=var)
main_pane.add(check)

# label = Label(main_pane, text="Preview:", sticky=W) --> error unknown option "-sticky"
label = Label(main_pane, text="Preview:")
main_pane.add(label)

entry = Text(main_pane, height=10, width=50)
main_pane.add(entry)
root.mainloop()

Result

I want to place my option check box and my preview label left-justified. I read in this documentation, there should be a sticky option that "functions like the sticky argument to the .grid() method." But this option does not work (see the two lines I commented out).

Is there a way to place some widgets left-justified or right-justified inside a vertically oriented paned window in tkinter? I'm using tkinter version 8.6 with Tcl/Tk version 8.6.9 on windows.

apio
  • 154
  • 11

1 Answers1

1

There is an option called anchor whihc functions similar to sticky

Add anchor parameter for the widget. e.g -

check = Checkbutton(main_pane, text="My option", variable=var,anchor=W)

You will see that the check button is towards the left.

The final code -

from tkinter import *

root = Tk()

root.resizable()
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
x_cordinate = int((screen_width / 2) - (300 / 2))
y_cordinate = int((screen_height / 2) - (200 / 2))
root.geometry("{}x{}+{}+{}".format(300, 200, x_cordinate, y_cordinate))
root.title("Pane Sticky Test")

main_pane = PanedWindow(root, orient=VERTICAL)
main_pane.pack(fill=BOTH, expand=1)

var = IntVar()

check = Checkbutton(main_pane, text="My option", variable=var,anchor=W)
main_pane.add(check)

label = Label(main_pane, text="Preview:",anchor=W)
main_pane.add(label)

entry = Text(main_pane, height=10, width=50)
main_pane.add(entry)
root.mainloop()

And do not use * for import. More recommended is this way - import tkinter as tk i.e using alias import

Reference - anchor and why use import tkinter

PCM
  • 2,881
  • 2
  • 8
  • 30