Tkinter Templet

Tkinter Templet

Postby dietrich41 » Mon Sep 28, 2015 12:41 am

If you would have to come up with a basic Tkinter app templet that can easily be modified for the start of a new program or just quick testing, what would you code?
My present favorite is ...
Code: Select all
a simple Tkinter app template for testing


    # Python2
    import Tkinter as tk
except ImportError:
    # Python3
    import tkinter as tk

class MyApp(tk.Tk):
    def __init__(self, title, width, height, x, y):
        # root window is self
        # set size and position of window
        self.geometry("{}x{}+{}+{}".format(width, height, x, y))


    def createWidgets(self):
        used grid layout manager for widgets
        txt = "Enter name:"
        tk.Label(self, text=txt, fg='blue').grid(row=0, column=0)

        self.entry = tk.Entry(self, width=25)
        self.entry.grid(row=0, column=1, pady=5)
        self.entry.bind('<Return>', self.showName)

        txt2 = "Click on Me!"
        self.button = tk.Button(self, text=txt2, command=self.action)
        self.button.grid(row=1, column=0, pady=5)

        txt3 = '-'*10
        self.label = tk.Label(self, text=txt3, fg='white', bg='red', width=30)
        self.label.grid(row=2, column=0, columnspan=2, pady=5)
        # make column 1 stretchable
        self.columnconfigure(1, weight=1)
        # put cursor in entry

    def action(self):
        the button has been clicked, do something
        # write text to the label
        self.label['text'] = "A good day to you!"

    def showName(self, event=None):
        the return key has been pressed in entry
        name = self.entry.get()
        sf = "You entered {}".format(name)
        self.label['text'] = sf

# set the parameters for class MyApp
title = "Tkinter basic class"
# width and height of root window
width = 300
height = 150
# Upper Left Corner coordinates (x, y) of root window
x = 100
y = 120
# create an instance of class myApp
app = MyApp(title, width, height, x, y)
# start the event loop
User avatar
Posts: 194
Joined: Sun Jul 26, 2015 2:10 am
Location: Reno NV

Return to Challenges

Who is online

Users browsing this forum: No registered users and 1 guest