我想要一个“不要再显示我”的窗口(一个顶层窗口)。如果选中此框,我不希望它再次向我显示此窗口。
import configparser
from tkinter import *
config = configparser.RawConfigParser() #my ini file
config.add_section('Section1')
config.set('Section1', 'a_bool', 'False')
with open('settings.ini', 'w') as configfile:
config.write(configfile)
root = Tk()
def var_states(): #write to ini file
global mt
print(config.read('ayrlar.ini'))
if var1 == True:
config.set('Section1', 'a_bool', 'True')
with open('settings.ini', 'w') as configfile:
config.write(configfile)
global window
window.destroy()
elif var1 == False:
config.set('Section1', 'a_bool', 'False')
with open('settings.ini', 'w') as configfile:
config.write(configfile)
global window
window.destroy()
var1 = config.getboolean('Section1', 'a_bool')
def show(): #if checkbox is true
global window #dont show
window= Toplevel(root)
Checkbutton(window, text="Don't show me again", variable=var1).place(x=0, y=0)
Button(window, text='Okey', command=var_states).place(x=0, y=25)
root.after(10,show)
root.mainloop()
再次运行时,我不想显示此窗口。如何使用ConfigParser
?
答案 0 :(得分:0)
要使Checkbutton
的变量起作用,它应该是IntVar
或BooleanVar
。因此,我将代码中的var1
替换为BooleanVar
:
def var_states(): #write to ini file
global mt
print(config.read('ayrlar.ini'))
if var1.get():
config.set('Section1', 'a_bool', 'True')
with open('settings.ini', 'w') as configfile:
config.write(configfile)
global window
window.destroy()
else:
config.set('Section1', 'a_bool', 'False')
with open('settings.ini', 'w') as configfile:
config.write(configfile)
global window
window.destroy()
var1 = BooleanVar(root, value=config.getboolean('Section1', 'a_bool'))