我需要帮助。当我保存“box”的值时,它只写“!checkbutton”而不是我想要的值。为什么会发生这种情况以及如何解决它。 所以我希望它在txt中写入onvalue,而不是。!勾选复选框时,选中按钮 如何解决这个问题 此外,如果有办法使复选框变大,我将不胜感激:)
# -*- 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()
尝试将功能更改为以下内容:
简言之,您忘了调用
var.get()
,而是说box
,这就是checkbutton本身另外,我将第二个
if
语句更改为elif
,因为有多个if
将导致第二个if
被执行,无论第一个if
上的条件是什么。只要试着用你的代码说一个空文件名,你就会发现问题所在希望这有帮助,如果有任何错误或疑问,请务必让我知道
相关问题 更多 >
编程相关推荐