I'm creating a GUI with widgets, buttons, tabs, notes, and eventually will control a servo motor. In this GUI, there is the option to take notes, I'm wanting to save all the conent of the GUI, including the notes, to reopen and continue later.
My code below is VERY crude... I'm new but I'm trying :) `
import tkinter
from tkinter import *
import tkinter as tk
from tkinter import ttk
from tkinter import filedialog
from tkinter import filedialog as fd
from tkinter.messagebox import showinfo
root = Tk()
root.title('Linear Motion System')
root.geometry("850x750")
# Below code is the tab structure of the GUI
##########################################################################################################
my_notebook = ttk.Notebook(root)
my_notebook.pack(pady=30)
my_frame1 = Frame(my_notebook, width=850, height=650, )
my_frame2 = Frame(my_notebook, width=850, height=650, )
my_frame3 = Frame(my_notebook, width=850, height=650, )
my_frame1.pack(fill="both", expand=1)
my_frame2.pack(fill="both", expand=1)
my_frame3.pack(fill="both", expand=1)
my_notebook.add(my_frame1, text="Testing")
my_notebook.add(my_frame2, text="Command History")
my_notebook.add(my_frame3, text="Notes")
# STRUCTURE OF GUI#######################################################################
# File Dialog Button
# helper function to select a file to open
def select_file():
filetypes = (
('text files', '*.txt'),
('All files', '*.*')
)
filename = fd.askopenfilename(
title='Open a file',
initialdir=(r"\"),
filetypes=filetypes)
showinfo(
title='Selected File',
message=filename
)
# save a file
def file_save():
filetypes = (
('text files', '*.txt'),
('All files', '*.*')
)
f = fd.asksaveasfile(
title='Save a file',
initialdir=(r"\"),
mode='wb',
defaultextension="*.*")
if f is None: # asksaveasfile return `None` if dialog closed with "cancel".
return
# save a file
def file_save_as():
filetypes = (
('text files', '*.txt'),
('All files', '*.*')
)
f = fd.asksaveasfile(
title='Save a file',
initialdir=(r"\"),
mode='w',
defaultextension="*.*") #*.* is all extensions?
if f is None: # asksaveasfile return `None` if dialog closed with "cancel".
return
def Click(e, var):
def VG(var):
select_file()
def G(var):
file_save()
def P(var):
file_save_as()
e.widget.focus()
nclst = [(' Open', lambda var=var: VG(var)),
(' Save', lambda var=var: G(var)),
(' Save As', lambda var=var: P(var)), ]
my_menu = tk.Menu(None, tearoff=0, takefocus=0)
for (txt, cmd) in nclst:
my_menu.add_command(label=txt, command=cmd)
my_menu.tk_popup(e.x_root + 10, e.y_root + 10, entry="0")
l_var = tk.StringVar()
lab = tk.Button(root, textvariable=l_var, width=6, height=1)
# lab = tk.Label(root, textvariable = l_var, width = 10)
l_var.set("File")
lab.bind('<Button-1>', lambda e, var=l_var: Click(e, var))
lab.place(x=0, y=0)
# This button is to open the SVX SERVO SUITE
# Servo Configuraiton/tuning
def openFile():
import os
os.startfile(r"\")
Button(my_frame1, text="SVX Servo Suite", font=('Helvetica 10'), command=openFile).place(x=350, y=153)
# This is for the Testing Tab
# Point to Point
Button(my_frame1, text="START", bg='green', font=('Helvetica 10')).place(x=525, y=361) # distance
Button(my_frame1, text="START", bg='green', font=('Helvetica 10')).place(x=140, y=410) # start
Button(my_frame1, text="START", bg='green', font=('Helvetica 10')).place(x=465, y=410) # middle
Button(my_frame1, text="START", bg='green', font=('Helvetica 10')).place(x=775, y=410) # end
# Continuous Loop
Button(my_frame1, text="START", bg='green', height=6, width=5, font=('Helvetica 10')).place(x=430, y=510)
Button(my_frame1, text="STOP", bg='red', height=6, width=5, font=('Helvetica 10')).place(x=480, y=510)
# Emergency Stop
Button(my_frame1, text="STOP", bg='red', height=2, width=15, font=('Helvetica 20 bold underline')).place(x=570, y=550)
Label(my_frame1, text="EMERGECY STOP", font=('Helvetica 10 bold')).place(x=649, y=528)
# Welcome label
Label(my_frame1, text="Connection Status:", font=('Helvetica 8')).place(x=135, y=30)
Label(my_frame1, text="Linear Motion System Software, 2023", font=('Helvetica 20 bold')).place(x=176, y=50)
Label(my_frame1, text="Science Team, 3440", font=('Helvetica 14')).place(x=329, y=90)
Label(my_frame1, text="Please note: Motor configuration/tuning must be performed in the SVX Servo Suite V1.0.20.0914",
fg='red', font=('Helvetica 14')).place(x=16, y=120)
Label(my_frame1, text="____________________________________________________________________________________",
font=('Helvetica 14')).place(y=180)
# Initialize parameters
Label(my_frame1, text="Initialize Parameters", font=('Helvetica 15 underline bold')).place(x=10, y=210)
Label(my_frame1, text="Velocity:", font=('Helvetica 15 ')).place(x=10, y=260)
Label(my_frame1, text="A value of 0 means the parameter has not been set", font=('Helvetica 10 ')).place(x=350, y=214)
Label(my_frame1, text="Acceleration:", font=('Helvetica 15 ')).place(x=205, y=260)
Label(my_frame1, text="Deceleration:", borderwidth=0, font=('Helvetica 15 ')).place(x=445, y=260)
Label(my_frame1, text="Direction:", font=('Helvetica 15 ')).place(x=680, y=260)
# Point to Point
Label(my_frame1, text="Point to Point Move", font=('Helvetica 15 underline bold')).place(x=10, y=310)
Label(my_frame1, text="Command Distance:", font=('Helvetica 15 ')).place(x=10, y=360)
Label(my_frame1, text="Unit:", font=('Helvetica 15 ')).place(x=340, y=360)
Label(my_frame1, text="Go to Start", font=('Helvetica 15 ')).place(x=10, y=410)
Label(my_frame1, text="Go to Middle", font=('Helvetica 15 ')).place(x=310, y=410)
Label(my_frame1, text="Go to End", font=('Helvetica 15 ')).place(x=655, y=410)
# Continuous Loop
Label(my_frame1, text="Continuous Loop", font=('Helvetica 15 underline bold')).place(x=10, y=460)
Label(my_frame1, text="Distance Traveled:", font=('Helvetica 15')).place(x=10, y=500)
Label(my_frame1, text="Distance Traveled from Start:", font=('Helvetica 15')).place(x=10, y=550)
Label(my_frame1, text="Distance Traveled from End:", font=('Helvetica 15')).place(x=10, y=600)
# Velocity drop down
Velocity = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18",
"19", "20", "21", "22", "23", "24", "25"]
variable = StringVar(root)
variable.set(Velocity[0]) # default value
w = OptionMenu(my_frame1, variable, *Velocity)
w.place(x=88, y=260)
# Acceleration drop down
Acceleration = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18",
"19", "20", "21", "22", "23", "24", "25"]
variable = StringVar(root)
variable.set(Acceleration[0]) # default value
w = OptionMenu(my_frame1, variable, *Acceleration)
w.place(x=320, y=260)
# Directoin drop down
Deceleration = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18",
"19", "20", "21", "22", "23", "24", "25"]
variable = StringVar(my_frame1)
variable.set(Deceleration[0]) # default value
w = OptionMenu(my_frame1, variable, *Deceleration)
w.place(x=560, y=260)
# Direction
Direction = ["CCW", "CW"]
variable = StringVar(my_frame1)
variable.set(Direction[0]) # default value
w = OptionMenu(my_frame1, variable, *Direction)
w.place(x=765, y=260)
# Text input for distance
distance_point = Entry(my_frame1, width=20, borderwidth=5).place(x=200, y=363)
# Text input for total distance
distance_point = Entry(my_frame1, width=20, borderwidth=5).place(x=190, y=503)
# Text input for distance from start
distance_point = Entry(my_frame1, width=20, borderwidth=5).place(x=280, y=553)
# Text input for distance from end
distance_point = Entry(my_frame1, width=20, borderwidth=5).place(x=270, y=603)
# unit
Unitofmeasure = ["Centimeter (cm)", "Millimeter (mm)", "Meter (m)", "Kilometer(km)", "Inches (in)", "Foot (ft)",
"Miles (mi)"]
variable = StringVar(my_frame1)
variable.set(Unitofmeasure[0]) # default value
w = OptionMenu(my_frame1, variable, *Unitofmeasure)
w.place(x=390, y=360)
# Velocity
UnitofVelocity = ["cm/s", "mm/s", "m/s", "km/hr", "in/s", "ft/s", "mph"]
variable = StringVar(my_frame1)
variable.set(UnitofVelocity[0]) # default value
w = OptionMenu(my_frame1, variable, *UnitofVelocity)
w.place(x=110, y=260)
# unit
UnitofAcc = ["cm/s/s", "mm/s/s", "m/s/s", "in/s/s", "ft/s/s"]
variable = StringVar(my_frame1)
variable.set(UnitofAcc[0]) # default value
w = OptionMenu(my_frame1, variable, *UnitofAcc)
w.place(x=340, y=260)
# unit
UnitofDEC = ["cm/s/s", "mm/s/s", "m/s/s", "in/s/s", "ft/s/s"]
variable = StringVar(my_frame1)
variable.set(UnitofDEC[0]) # default value
w = OptionMenu(my_frame1, variable, *UnitofDEC)
w.place(x=580, y=260)
# Above code is for the Home Tab
# This code is for the Notes Tab
# Notes Button
text = Text(my_frame3, width=103, height=40, ).place(x=10, y=10)
#This is for the Command History Tab
text = Text(my_frame2, width=103, height=40, ).place(x=10, y=10)
# ----------------------------------------------------------------------------------------------------------------------------------------------
###############################################################################################################################
root.mainloop()
` Thank you
I have setup a file dialog with "Open" "Save" and "Save As" and now I have to figure out how to correctly save the GUI.