Created
August 15, 2024 17:48
-
-
Save CoreyMSchafer/09ac42ad88a3ca963fb1452e679a9195 to your computer and use it in GitHub Desktop.
Python Tkinter Tutorial - Part 2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import tkinter as tk | |
from tkinter import ttk | |
def main(): | |
app = Application() | |
app.mainloop() | |
class Application(tk.Tk): | |
def __init__(self): | |
super().__init__() | |
self.title("Simple App") | |
self.columnconfigure(0, weight=1) | |
self.columnconfigure(1, weight=3) | |
self.rowconfigure(0, weight=1) | |
frame = InputForm(self) | |
frame.grid(row=0, column=0, sticky="nsew", padx=5, pady=5) | |
frame2 = InputForm(self) | |
frame2.grid(row=0, column=1, sticky="nsew", padx=5, pady=5) | |
class InputForm(ttk.Frame): | |
def __init__(self, parent): | |
super().__init__(parent) | |
self.columnconfigure(0, weight=1) | |
self.rowconfigure(1, weight=1) | |
self.entry = ttk.Entry(self) | |
self.entry.grid(row=0, column=0, sticky="ew") | |
self.entry.bind("<Return>", self.add_to_list) | |
self.entry_btn = ttk.Button(self, text="Add", command=self.add_to_list) | |
self.entry_btn.grid(row=0, column=1) | |
self.entry_btn2 = ttk.Button(self, text="Clear", command=self.clear_list) | |
self.entry_btn2.grid(row=0, column=2) | |
self.text_list = tk.Listbox(self) | |
self.text_list.grid(row=1, column=0, columnspan=3, sticky="nsew") | |
def add_to_list(self, _event=None): | |
text = self.entry.get() | |
if text: | |
self.text_list.insert(tk.END, text) | |
self.entry.delete(0, tk.END) | |
def clear_list(self): | |
self.text_list.delete(0, tk.END) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Great Tutorial! Thanks!