How to make resizable widgets in tkinter

This is a script to make resizable buttons in tkinter for a ‘responsible‘ app, i.e. a GUI that adapts to the resizable window madewith python and tkinter. It is very easy:

The resizable window with responsive widgets
The buttons adapt to the window as its resized
import tkinter as tk


class Root:
	def __init__(self):
		self.root = tk.Tk()
		self.widgets()

	def widgets(self):
		button1 = tk.Button(self.root, text ="Hello", fg="blue", bg="gold")
		button1.place(anchor=tk.E, relheight=0.25, relwidth=0.25, relx=0.4, rely = 0.5)
		
		button2 = tk.Button(self.root,text="World", bg="pink", fg = "green")
		button2.place(anchor=tk.W, relheight=0.25, relwidth=0.25, relx=0.6, rely= 0.5)


def main():
	app = Root()
	app.root.mainloop()

main()

A little of abstraction…

import tkinter as tk


class Root:
	def __init__(self):
		self.root = tk.Tk()
		self.widgets()

	def button_resizable(self, text, fg, bg, pos="est", relx=None):
		''' to create a resizable buttons '''
		# es: self.button1 = self.button_resizable(text="Hello", fg="blue", bg="gold", pos="est")
		
		button1 = tk.Button(self.root, text =text, fg=fg, bg=bg)
		if pos=="est":
			pos = tk.E
			if relx == None:
				relx=0.4 # from 0 to 1
		elif pos=="west":
			pos = tk.W
			if relx == None:
				relx=0.6
		button1.place(anchor=pos,
			relheight=0.25, relwidth=0.25, relx=relx, rely = 0.5)


	def widgets(self):
		''' widgets '''
		self.button1 = self.button_resizable(text="Hello", fg="blue", bg="gold", pos="est")
		self.button2 = self.button_resizable(text="World", fg="blue", bg="cyan", pos="west")


def main():
	app = Root()
	app.root.mainloop()


main()

Subscribe to the newsletter for updates
Tkinter templates
Avatar My youtube channel

Twitter: @pythonprogrammi - python_pygame

Videos

Speech recognition game

Pygame's Platform Game

Other Pygame's posts

Published by pythonprogramming

Started with basic on the spectrum, loved javascript in the 90ies and python in the 2000, now I am back with python, still making some javascript stuff when needed.