Answers for "python tkinter class"

8

tkinter basic

from tkinter import Tk, Label, Button

class MyFirstGUI:
    def __init__(self, master):
        self.master = master
        master.title("A simple GUI")

        self.label = Label(master, text="This is our first GUI!")
        self.label.pack()

        self.greet_button = Button(master, text="Greet", command=self.greet)
        self.greet_button.pack()

        self.close_button = Button(master, text="Close", command=master.quit)
        self.close_button.pack()

    def greet(self):
        print("Greetings!")

root = Tk()
my_gui = MyFirstGUI(root)
root.mainloop()
Posted by: Guest on May-15-2020
0

how to create a python class in tkinter

# Import
from tkinter import *
# Class Section
class Window(object):
  def __init__(self):
    self.tk = Tk()
    self.tk.title("Demo Window")
    self.tk.geometry("500x500")
    self.tk.resizable(0, 0)
    self.tk.mainloop()
    self.widgets()
    pass
  def widgets(self):
    lbl = Label(self.tk, text="Hello")
    lbl.pack()
    pass
win = Window()
Posted by: Guest on June-12-2021
2

self.app = Tk()

# Use Tkinter for python 2, tkinter for python 3
import tkinter as tk

class MainApplication(tk.Frame):
    def __init__(self, parent, *args, **kwargs):
        tk.Frame.__init__(self, parent, *args, **kwargs)
        self.parent = parent

        <create the rest of your GUI here>

if __name__ == "__main__":
    root = tk.Tk()
    MainApplication(root).pack(side="top", fill="both", expand=True)
    root.mainloop()
Posted by: Guest on May-13-2020

Python Answers by Framework

Browse Popular Code Answers by Language