python - 如果复选框在 Tkinter 上,则保存它的值
问题描述
我需要帮助。当我保存“box”的值时,它只写“.!checkbutton”而不是我想要的值。为什么会发生这种情况以及如何解决它。所以我希望它在 txt 中写入 onvalue 而不是 .!checkbutton 如果勾选了该框如何解决这个问题如果有办法使复选框更大,我将不胜感激:)
# -*- coding: utf8 -*-
from tkinter import *
from tkinter import messagebox
import os
root = Tk()
root.title('Feedback')
root.geometry("400x400")
organizerlist = [
"213",
"1231",
"123",
"123",
"123",
"123",
"123",
"."
]
eventsnames = [
"Rocket League"
]
def run():
# getting the name
filename = filename_ent.get()
if filename == '':
# to show an error that boxes are empty
messagebox.showerror(
'File exists', 'File already exists, try some other name thas is not used before')
if os.path.exists(f'{filename}.txt'):
# to show an error if the file already exists
messagebox.showerror(
'File exists', 'File already exists, try some other name not used before')
else:
# to open the file for python
new = open(f'{filename}.txt', '+w', encoding='utf-8')
# to write the name and email inside the file
new.write(f'''Admin of the event: {clicked.get()}\n{responsible.get()} \n{box}''')
lfilename.config(text=f'The Feedback has been written successfully!') # to change the label to the name
os.startfile(f'{filename}.txt') # to open the file in a new window
lfilename = Label(root,text="What do you want to call the file?", font = ("helvatica, 14"))
lfilename.grid(row=0, column=0)
filename_ent = Entry(root)
filename_ent.grid(row=1, column=0)
clicked = StringVar()
clicked.set("Who is Admin?")
drop = OptionMenu(root, clicked, "User", "@User2", "@User3", "@User")
drop.grid(row=2, column=0)
eventname = StringVar()
eventname.set("Which event was it?" )
eventname_drop = OptionMenu(root, eventname, *eventsnames, )
eventname_drop.grid(row=3,column=0, )
responsible = StringVar()
responsible.set("Who is the responsible?")
responsible_drop = OptionMenu(root, responsible, *organizerlist)
responsible_drop.grid(row=4,column=0)
responsible_drop.config(width=30, font=("helvatica", 14))
var = StringVar()
box = Checkbutton(root,text="test", variable=var, onvalue="Working", offvalue="Not Working")
box.grid(row=3,column=3)
b = Button(root, text='Done', command=run)
b.grid(row=20, column=0)
root.mainloop()
解决方案
尝试将功能更改为以下:
def run():
# getting the name
filename = filename_ent.get()
if filename == '':
# to show an error that boxes are empty
messagebox.showerror('Empty Box', 'Make sure to to fill the filename box.')
elif os.path.exists(f'{filename}.txt'):
# to show an error if the file already exists
messagebox.showerror(
'File exists', 'File already exists, try some other name not used before')
else:
# to open the file for python
new = open(f'{filename}.txt', '+w', encoding='utf-8')
# to write the name and email inside the file
new.write(f'Admin of the event: {clicked.get()}\n{responsible.get()} \n{var.get()}')
lfilename.config(text=f'The Feedback has been written successfully!') # to change the label to the name
os.startfile(f'{filename}.txt') # to open the file in a new window
简而言之,你忘了打电话var.get()
,而是说box
,这是检查按钮本身。
此外,我已将您的第二个if
语句更改为,因为无论第一个条件是什么,elif
都有多个if
将导致第二个被执行。只需尝试在您的代码中说出一个空文件名,您就可以看到问题所在。if
if
希望这会有所帮助,如果有任何错误或疑问,请告诉我。
推荐阅读
- android - Calendarview OnselectedDay 更改为时间戳
- swift - 检测返回视图控制器
- google-apps-script - 将电子表格另存为给定文件夹中的 csv 文件
- python - 类生成器定义
- mysql - 添加新行 created_at 字段填充 -3 小时差异
- python - 在tensorflow中逐行读取文本时的无限循环
- google-apps-script - 如何在返回未定义时返回字符串
- python - 使用 Scapy 从 PCAP 中提取有效负载数据
- windows - MdapiDeployCommand 找不到名称的组织配置
- html - 背景图像伪引号问题