这是我第一次在 Python 中使用 tkinter 和 GUI,所以我几乎是一个新手。
我有一个用 tkinter 制作的大 GUI 表单。它使用所有经典的 tkinter 小部件:文本输入框、旋转框、选项菜单、单选按钮和复选按钮。我想要做的是让用户在 GUI 表单中输入数据,然后按“保存”按钮将所有内容保存到文本文件中。不幸的是,我找不到很多将这样的数据保存到文本文件的示例。这是我的代码的通用示例。
import tkinter as tk
from tkinter import ttk
variables = dict()
root = tk.Tk()
root.title('Generic Form')
root.columnconfigure(0, weight=1)
ttk.Label(root, text='Generic Form', font=("TkDefaultFont", 16)).grid()
drf = ttk.Frame(root)
drf.grid(padx=10, sticky=(tk.N + tk.S))
drf.columnconfigure(0, weight=1)
g_info = ttk.LabelFrame(drf, text='Generic Data')
g_info.grid(row=0, column=0, sticky=(tk.W + tk.E))
variables['Scenario ID'] = tk.StringVar()
ttk.Label(g_info, text='Scenario ID').grid(row=0, column=0)
ttk.Entry(g_info, textvariable=variables['Scenario ID']).grid(row=1, column=0, sticky=(tk.W + tk.E))
variables['Integer Value'] = tk.IntVar()
ttk.Label(g_info, text='Integer Value').grid(row=2, column=0)
ttk.Spinbox(g_info, textvariable=variables['Integer Value'], from_=0, to=100, increment = 1).grid(row=3, column=0, sticky=(tk.W + tk.E))
variables['OPTIONS'] = tk.StringVar()
option_var = tk.StringVar(value='Choose')
choices = ('This', 'That', 'The Other Thing')
ttk.Label(g_info, text='OPTIONS').grid(row=4, column=0, sticky=(tk.W + tk.E))
ttk.OptionMenu(g_info, option_var, *choices).grid(row=5, column=0, sticky=(tk.W + tk.E))
choice_default = tk.StringVar(value=F)
variables['CHOICE'] = tk.StringVar()
choice_frame = ttk.Frame(g_info)
ttk.Label(g_info, text='CHOICE').grid(row=6, column=0, sticky=(tk.W + tk.E))
choice_frame.grid(row=7, column=0, sticky=(tk.W + tk.E))
for choice in ('T', 'F'):
ttk.Radiobutton(choice_frame, value=choice, test=choice, variable=choice_default.pack()
buttons = tk.Frame(drf)
buttons.grid(column=1, pady=20, sticky=(tk.W + tk.E))
save_button = ttk.Button(buttons, test='Save')
save_button.pack(side=tk.RIGHT)
def on_save():
filename = f'C:/test.txt'
data = dict()
with open(filename, 'w', newline='' as fh:
fh.write("\n")
save_button.configure(command=on_save)
root.mainloop()
这是我想要获取的输出文本。
Generic Data
Scenario ID = Scenario 1
Integer Value = 51
Options = The Other Thing
Choice = T
我对 tkinter 的了解大部分来自 Alan D. Moore 的书 使用 Tkinter 进行 Python GUI 编程。不幸的是,本书只描述了如何将数据保存到 CSV 文件中。对于我正在从事的项目,我需要将其保存在文本文件中。我知道有一种方法可以做到这一点,但除了 Entry 小部件之外,我找不到任何示例。
import tkinter as tk
from tkinter import ttk
variables = dict()
root = tk.Tk()
root.title('Generic Form')
root.columnconfigure(0, weight=1)
ttk.Label(root, text='Generic Form', font=("TkDefaultFont", 16)).grid()
drf = ttk.Frame(root)
drf.grid(padx=10, sticky=(tk.N + tk.S))
drf.columnconfigure(0, weight=1)
g_info = ttk.LabelFrame(drf, text='Generic Data')
g_info.grid(row=0, column=0, sticky=(tk.W + tk.E))
variables['Scenario ID'] = tk.StringVar()
ttk.Label(g_info, text='Scenario ID').grid(row=0, column=0)
ttk.Entry(g_info, textvariable=variables['Scenario ID']).grid(row=1, column=0, sticky=(tk.W + tk.E))
variables['Integer Value'] = tk.IntVar()
ttk.Label(g_info, text='Integer Value').grid(row=2, column=0)
ttk.Spinbox(g_info, textvariable=variables['Integer Value'], from_=0, to=100, increment = 1).grid(row=3, column=0, sticky=(tk.W + tk.E))
variables['OPTIONS'] = tk.StringVar()
option_var = tk.StringVar(value='Choose')
choices = ('This', 'That', 'The Other Thing')
ttk.Label(g_info, text='OPTIONS').grid(row=4, column=0, sticky=(tk.W + tk.E))
ttk.OptionMenu(g_info, option_var, *choices).grid(row=5, column=0, sticky=(tk.W + tk.E))
choice_default = tk.StringVar(value='F')
variables['CHOICE'] = tk.StringVar()
choice_frame = ttk.Frame(g_info)
ttk.Label(g_info, text='CHOCE').grid(row=6, column=0, sticky=(tk.W + tk.E))
choice_frame.grid(row=7, column=0, sticky=(tk.W + tk.E))
for choice in 'TF':
ttk.Radiobutton(choice_frame, value=choice, variable=choice_default).pack()
def save():
scen = variables['Scenario ID'].get()
intval = variables['Integer Value'].get()
opt = variables['OPTIONS'].get()
choice = variables['CHOICE'].get()
print( "Generic Data" )
print( f" Scenario ID = {scen}" )
print( f" Integer Value = {intval}" )
print( f" Options = {opt}" )
print( f" Choice = {choice}" )
tk.Button( g_info, text="Save", command=save).grid(row=7, column=0)
root.mainloop()
import tkinter as tk
from tkinter import ttk
variables = dict()
root = tk.Tk()
root.title('Generic Form')
root.columnconfigure(0, weight=1)
ttk.Label(root, text='Generic Form', font=("TkDefaultFont", 16)).grid()
drf = ttk.Frame(root)
drf.grid(padx=10, sticky=(tk.N + tk.S))
drf.columnconfigure(0, weight=1)
frame_info = ttk.LabelFrame(drf, text='Generic Data')
frame_info.grid(row=0, column=0, sticky=(tk.W + tk.E))
variables['Scenario ID'] = tk.StringVar()
ttk.Label(frame_info, text='Scenario ID').grid(row=0, column=0)
ttk.Entry(frame_info, textvariable=variables['Scenario ID']).grid(row=1, column=0, sticky=(tk.W + tk.E))
variables['Integer Value'] = tk.IntVar()
ttk.Label(frame_info, text='Integer Value').grid(row=2, column=0)
ttk.Spinbox(frame_info, textvariable=variables['Integer Value'], from_=0, to=100, increment = 1).grid(row=3, column=0, sticky=(tk.W + tk.E))
variables['OPTIONS'] = tk.StringVar()
option_var = tk.StringVar(value='Choose')
choices = ('Choose', 'This', 'That', 'The Other Thing')
ttk.Label(frame_info, text='OPTIONS').grid(row=4, column=0, sticky=(tk.W + tk.E))
ttk.OptionMenu(g_info, variables['OPTIONS'], *choices).grid(row=5, column=0, sticky=(tk.W + tk.E))
choice_default = tk.StringVar(value='F')
variables['CHOICE'] = tk.StringVar()
choice_frame = ttk.Frame(frame_info)
ttk.Label(frame_info, text='CHOICE').grid(row=6, column=0, sticky=(tk.W + tk.E))
choice_frame.grid(row=7, column=0, sticky=(tk.W + tk.E))
for choice in ('T', 'F'):
ttk.Radiobutton(choice_frame, value=choice, variable=variables['CHOICE']).pack()
def on_save():
inc = variables['Scenario ID'].get()
number = variables['Integer Value'].get()
menu = variables['OPTIONS'].get()
boolean = variables['CHOICE'].get()
with open('GUI.txt', 'w') as x:
x.write( 'Generic Data' ), x.write('\n')
x.write( f' Scenario ID = {inc}' ), x.write('\n')
x.write( f' Integer Value = {number}' ), x.write('\n')
x.write( f' Options = {menu}' ), x.write('\n')
x.write( f' Choice = {boolean}' ), x.write('\n')
tk.Button(frame_info, text='Save', command=on_save).grid(row=8, column=0, sticky=(tk.W + tk.E))
root.mainloop()