Radiobutton预设不会在Tkinter中携带

时间:2016-11-02 01:25:56

标签: python tkinter radio-button

我正在尝试创建一个从文本文件中读取设置的设置页面,并将单选按钮变量设置为这些。当我单独运行时,该功能完美运行。但是,当我在另一个tkinter程序中运行时,预设设置不会显示。我错过了什么?提前致谢。

from Tkinter import *

def format():
    root = Tk()
    root.geometry("800x480+400+240")
    root.configure(background='#9E9E9E')

    time_format = IntVar()
    time_format.set(12)

    Time_Format1 = Radiobutton(root, text="12 Hour Format(AM/PM)", font="georgia 12 bold", bg="#1E88E5", activebackground="#64B5F6", fg="black", activeforeground="black", highlightbackground="#212121", variable=time_format, value=12, indicatoron=0)

    Time_Format2 = Radiobutton(root, text="24 Hour Format", font="georgia 12 bold", bg="#1E88E5", activebackground="#64B5F6", fg="black", activeforeground="black", highlightbackground="#212121", variable=time_format, value=24, indicatoron=0)

    Time_Format1.grid(row=0,column=0)
    Time_Format2.grid(row=1,column=0)

    root.mainloop()

root = Tk()
root.geometry("800x480+400+240")
root.configure(background='#9E9E9E')


Open_time = Button(root, text="Open Date & Time settings", command=format)
Open_time.grid(row=0, column=0)
root.mainloop()

1 个答案:

答案 0 :(得分:1)

Tkinter只能有一个主窗口 - Tk()
但它可能有许多对话框窗口或使用Toplevel()创建的其他(子)窗口。

Tkinter只能有一个mainloop()

from Tkinter import *

def format():
    root = Toplevel()
    root.geometry("800x480+400+240")
    root.configure(background='#9E9E9E')

    time_format = IntVar()
    time_format.set(12)

    time_format1 = Radiobutton(root, text="12 Hour Format(AM/PM)", font="georgia 12 bold", bg="#1E88E5", activebackground="#64B5F6", fg="black", activeforeground="black", highlightbackground="#212121", variable=time_format, value=12, indicatoron=0)
    time_format2 = Radiobutton(root, text="24 Hour Format", font="georgia 12 bold", bg="#1E88E5", activebackground="#64B5F6", fg="black", activeforeground="black", highlightbackground="#212121", variable=time_format, value=24, indicatoron=0)

    time_format1.grid(row=0,column=0)
    time_format2.grid(row=1,column=0)


root = Tk()
root.geometry("800x480+400+240")
root.configure(background='#9E9E9E')

open_time = Button(root, text="Open Date & Time settings", command=format)
open_time.grid(row=0, column=0)

root.mainloop()