some online examples of passing variables between windows
This commit is contained in:
parent
ece8f8ad7d
commit
74acacd80c
30
tkinter-example-enter.py
Normal file
30
tkinter-example-enter.py
Normal file
|
@ -0,0 +1,30 @@
|
|||
import tkinter as tk
|
||||
|
||||
class Demo1:
|
||||
def __init__(self, master):
|
||||
self.master = master
|
||||
self.frame = tk.Frame(self.master)
|
||||
self.button1 = tk.Button(self.frame, text = 'New Window', width = 25, command = self.new_window)
|
||||
self.button1.pack()
|
||||
self.frame.pack()
|
||||
def new_window(self):
|
||||
self.newWindow = tk.Toplevel(self.master)
|
||||
self.app = Demo2(self.newWindow)
|
||||
|
||||
class Demo2:
|
||||
def __init__(self, master):
|
||||
self.master = master
|
||||
self.frame = tk.Frame(self.master)
|
||||
self.quitButton = tk.Button(self.frame, text = 'Quit', width = 25, command = self.close_windows)
|
||||
self.quitButton.pack()
|
||||
self.frame.pack()
|
||||
def close_windows(self):
|
||||
self.master.destroy()
|
||||
|
||||
def main():
|
||||
root = tk.Tk()
|
||||
app = Demo1(root)
|
||||
root.mainloop()
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
31
tkinter-example-oop-1.py
Normal file
31
tkinter-example-oop-1.py
Normal file
|
@ -0,0 +1,31 @@
|
|||
# From: https://www.pythontutorial.net/tkinter/tkinter-object-oriented-window/
|
||||
|
||||
import tkinter as tk
|
||||
from tkinter import ttk
|
||||
from tkinter.messagebox import showinfo
|
||||
|
||||
class App(tk.Tk):
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
|
||||
self.title('test')
|
||||
self.geometry('300x50')
|
||||
self.resizable(0,0)
|
||||
self.columnconfigure(0, weight=1)
|
||||
self.columnconfigure(1, weight=1)
|
||||
self.columnconfigure(2, weight=1)
|
||||
self.columnconfigure(3, weight=1)
|
||||
|
||||
self.label = ttk.Label(self, text='testing label')
|
||||
self.label.grid(column=1, row=0, columnspan=2, sticky='nsew')
|
||||
|
||||
self.button = ttk.Button(self, text='testing button')
|
||||
self.button['command'] = self.button_clicked
|
||||
self.button.grid(column=1, row=1, columnspan=2, sticky='nsew')
|
||||
|
||||
def button_clicked(self):
|
||||
showinfo(title='Information', message='testing button method')
|
||||
|
||||
if __name__ == "__main__":
|
||||
app = App()
|
||||
app.mainloop()
|
97
tkinter-example-oop-2.py
Normal file
97
tkinter-example-oop-2.py
Normal file
|
@ -0,0 +1,97 @@
|
|||
# from: https://www.pythontutorial.net/tkinter/tkinter-object-oriented-frame/
|
||||
|
||||
import tkinter as tk
|
||||
from tkinter import ttk
|
||||
|
||||
|
||||
class InputFrame(ttk.Frame):
|
||||
def __init__(self, container):
|
||||
super().__init__(container)
|
||||
# setup the grid layout manager
|
||||
self.columnconfigure(0, weight=1)
|
||||
self.columnconfigure(0, weight=3)
|
||||
|
||||
self.__create_widgets()
|
||||
|
||||
def __create_widgets(self):
|
||||
# Find what
|
||||
ttk.Label(self, text="Find what:").grid(column=0, row=0, sticky=tk.W)
|
||||
keyword = ttk.Entry(self, width=30)
|
||||
keyword.focus()
|
||||
keyword.grid(column=1, row=0, sticky=tk.W)
|
||||
|
||||
# Replace with:
|
||||
ttk.Label(self, text="Replace with:").grid(column=0, row=1, sticky=tk.W)
|
||||
replacement = ttk.Entry(self, width=30)
|
||||
replacement.grid(column=1, row=1, sticky=tk.W)
|
||||
|
||||
# Match Case checkbox
|
||||
match_case = tk.StringVar()
|
||||
match_case_check = ttk.Checkbutton(
|
||||
self,
|
||||
text="Match case",
|
||||
variable=match_case,
|
||||
command=lambda: print(match_case.get()),
|
||||
)
|
||||
match_case_check.grid(column=0, row=2, sticky=tk.W)
|
||||
|
||||
# Wrap Around checkbox
|
||||
wrap_around = tk.StringVar()
|
||||
wrap_around_check = ttk.Checkbutton(
|
||||
self,
|
||||
variable=wrap_around,
|
||||
text="Wrap around",
|
||||
command=lambda: print(wrap_around.get()),
|
||||
)
|
||||
wrap_around_check.grid(column=0, row=3, sticky=tk.W)
|
||||
|
||||
for widget in self.winfo_children():
|
||||
widget.grid(padx=0, pady=5)
|
||||
|
||||
|
||||
class ButtonFrame(ttk.Frame):
|
||||
def __init__(self, container):
|
||||
super().__init__(container)
|
||||
# setup the grid layout manager
|
||||
self.columnconfigure(0, weight=1)
|
||||
|
||||
self.__create_widgets()
|
||||
|
||||
def __create_widgets(self):
|
||||
ttk.Button(self, text="Find Next").grid(column=0, row=0)
|
||||
ttk.Button(self, text="Replace").grid(column=0, row=1)
|
||||
ttk.Button(self, text="Replace All").grid(column=0, row=2)
|
||||
ttk.Button(self, text="Cancel").grid(column=0, row=3)
|
||||
|
||||
for widget in self.winfo_children():
|
||||
widget.grid(padx=0, pady=3)
|
||||
|
||||
|
||||
class App(tk.Tk):
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
self.title("Replace")
|
||||
self.geometry("400x150")
|
||||
self.resizable(0, 0)
|
||||
# windows only (remove the minimize/maximize button)
|
||||
# self.attributes('-toolwindow', True)
|
||||
|
||||
# layout on the root window
|
||||
self.columnconfigure(0, weight=4)
|
||||
self.columnconfigure(1, weight=1)
|
||||
|
||||
self.__create_widgets()
|
||||
|
||||
def __create_widgets(self):
|
||||
# create the input frame
|
||||
input_frame = InputFrame(self)
|
||||
input_frame.grid(column=0, row=0)
|
||||
|
||||
# create the button frame
|
||||
button_frame = ButtonFrame(self)
|
||||
button_frame.grid(column=1, row=0)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
app = App()
|
||||
app.mainloop()
|
27
tkinter-example-passing-variables-between-windows-2.py
Normal file
27
tkinter-example-passing-variables-between-windows-2.py
Normal file
|
@ -0,0 +1,27 @@
|
|||
import tkinter as tk
|
||||
|
||||
|
||||
class Application(tk.Frame):
|
||||
def __init__(self):
|
||||
self.root = tk.Tk()
|
||||
self.root.geometry("300x200")
|
||||
|
||||
tk.Frame.__init__(self, self.root)
|
||||
self.create_widgets()
|
||||
|
||||
def create_widgets(self):
|
||||
self.root.bind("<Return>", self.parse)
|
||||
self.grid()
|
||||
|
||||
self.submit = tk.Button(self, text="Submit")
|
||||
self.submit.bind("<Button-1>", self.parse)
|
||||
self.submit.grid()
|
||||
|
||||
def parse(self, event):
|
||||
print("You clicked?")
|
||||
|
||||
def start(self):
|
||||
self.root.mainloop()
|
||||
|
||||
|
||||
Application().start()
|
69
tkinter-example-passing-variables-between-windows-3.py
Normal file
69
tkinter-example-passing-variables-between-windows-3.py
Normal file
|
@ -0,0 +1,69 @@
|
|||
# https://stackoverflow.com/questions/70614386/passing-values-tkinter-elements-between-classes-in-tkinter
|
||||
|
||||
import os
|
||||
import tkinter as tk
|
||||
from tkinter import filedialog
|
||||
|
||||
|
||||
class MainFrame:
|
||||
def __init__(self, master):
|
||||
self.file_selection = FileSelect(master, main=self)
|
||||
self.file_load = LoadFiles(master, main=self)
|
||||
|
||||
|
||||
class FileSelect:
|
||||
def __init__(self, master, main):
|
||||
self.main = main
|
||||
self.files = []
|
||||
|
||||
# Frame
|
||||
self.frame = tk.LabelFrame(master, text="File Selection")
|
||||
self.frame.grid(row=0, column=0)
|
||||
|
||||
# Select directory button
|
||||
tk.Button(self.frame, text="Open:", command=self.directory_path).grid(
|
||||
row=0, column=0
|
||||
)
|
||||
|
||||
# Directory Selection
|
||||
def directory_path(self):
|
||||
|
||||
# Select filepath
|
||||
directory = filedialog.askdirectory(initialdir="/", title="Select a directory")
|
||||
os.chdir(directory)
|
||||
|
||||
# Populate list of files
|
||||
for tb in os.listdir(directory):
|
||||
self.files.append(tb)
|
||||
|
||||
# Update GUI, activate button in different class if condition met
|
||||
if len(os.listdir(directory)) == 0:
|
||||
self.main.file_load.import_files_button["state"] = tk.DISABLED
|
||||
|
||||
else:
|
||||
self.main.file_load.import_files_button["state"] = tk.NORMAL
|
||||
|
||||
|
||||
class LoadFiles:
|
||||
def __init__(self, master, main):
|
||||
self.main = main
|
||||
|
||||
# Frame
|
||||
self.frame = tk.LabelFrame(master, text="File loading")
|
||||
self.frame.grid(row=1, column=0)
|
||||
# Load button
|
||||
self.import_files_button = tk.Button(
|
||||
self.frame, text="Import TB", command=self.print_files, state=tk.DISABLED
|
||||
)
|
||||
self.import_files_button.grid(row=0, column=0, sticky="EW")
|
||||
|
||||
# Tkinter Elements...
|
||||
|
||||
def print_files(self):
|
||||
print(self.main.file_selection.files)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
root = tk.Tk()
|
||||
MainFrame(root)
|
||||
root.mainloop()
|
82
tkinter-example-passing-variables-between-windows.py
Normal file
82
tkinter-example-passing-variables-between-windows.py
Normal file
|
@ -0,0 +1,82 @@
|
|||
import os
|
||||
import tkinter as tk
|
||||
from tkinter import filedialog
|
||||
|
||||
|
||||
class MainFrame:
|
||||
|
||||
def __init__(self, master):
|
||||
self.master = master
|
||||
|
||||
# Variables
|
||||
self.file_list = [] # Files list
|
||||
self.import_state = tk.DISABLED # Enable import on correct fp
|
||||
|
||||
# Class Frames
|
||||
FileSelect(master, self.file_list, self.import_state)
|
||||
LoadFiles(master, self.file_list, self.import_state)
|
||||
|
||||
|
||||
class FileSelect:
|
||||
|
||||
def __init__(self, master, files, button_state):
|
||||
self.files = files
|
||||
self.button_state = button_state
|
||||
|
||||
# Frame
|
||||
self.frame = tk.LabelFrame(master, text="File Selection")
|
||||
self.frame.grid(row=0, column=0)
|
||||
|
||||
# Select directory button
|
||||
tk.Button(self.frame, text="Open:", command=self.directory_path).grid(
|
||||
row=0, column=0
|
||||
)
|
||||
|
||||
# Tkinter Elements...
|
||||
|
||||
# Directory Selection
|
||||
def directory_path(self):
|
||||
|
||||
# Select filepath
|
||||
directory = filedialog.askdirectory(initialdir="/", title="Select a directory")
|
||||
os.chdir(directory)
|
||||
|
||||
# Populate list of files
|
||||
for tb in os.listdir(directory):
|
||||
self.files.append(tb)
|
||||
|
||||
# Update GUI, activate button in different class if condition met
|
||||
if len(os.listdir(directory)) == 0:
|
||||
# Update GUI Labels...
|
||||
self.button_state = tk.DISABLED
|
||||
print(self.files, self.button_state)
|
||||
elif len(self.files) == len(os.listdir(directory)):
|
||||
# Update GUI Labels...
|
||||
self.button_state = tk.NORMAL
|
||||
print(self.files, self.button_state)
|
||||
|
||||
|
||||
class LoadFiles:
|
||||
def __init__(self, master, files, button_state):
|
||||
self.files = files
|
||||
self.button_state = button_state
|
||||
|
||||
# Frame
|
||||
self.frame = tk.LabelFrame(master, text="File loading")
|
||||
self.frame.grid(row=1, column=0)
|
||||
# Load button
|
||||
self.import_files_button = tk.Button(
|
||||
self.frame, text="Import TB", command=self.print_files
|
||||
)
|
||||
self.import_files_button.grid(row=0, column=0, sticky="EW")
|
||||
|
||||
# Tkinter Elements...
|
||||
|
||||
def print_files(self):
|
||||
print(self.files, self.button_state)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
root = tk.Tk()
|
||||
MainFrame(root)
|
||||
root.mainloop()
|
Loading…
Reference in New Issue
Block a user