Python tkinter radiobutton小部件在单个窗口中工作,但不在多个窗口中

时间:2018-04-22 13:59:35

标签: python tkinter

我有一个问题,我试图解决了很长一段时间,但到目前为止没有成功。当我在一个窗口中使用下面的代码和radiobutton时 - 一切都很完美!我确切地知道哪个单体按下了。

import tkinter as tk
from tkinter import *
from tkinter import ttk

def PressedRadio(var):
    global selection
    selection = var.get()
    print('Pushed the button!')
    print('var has value', selection)
    text_dict = {0: 'Nothing selected!', 1: 'Profile 1 selected', 2: 'Profile 2 selected', 3: 'Profile 3 selected',
                 4: 'profile 4 selected'}
    text_to_print = text_dict[selection]
    print(text_to_print)

def tutorial():

    tut = tk.Tk()
    tut.wm_title("Tutorial")
    label=ttk.Label(tut, text="What do you need help with?")

    var = IntVar()
    profile_sel_1 = ttk.Radiobutton(tut, text='Profile Selection_1', variable=var, value=1)
    profile_sel_2 = ttk.Radiobutton(tut, text='Profile Selection_2', variable=var, value=2)
    profile_sel_3 = ttk.Radiobutton(tut, text='Profile Selection_3', variable=var, value=3)
    profile_sel_4 = ttk.Radiobutton(tut, text='Profile Selection_4', variable=var, value=4)
    ser_port_btn = ttk.Button(tut, text='ENTER', command=lambda:PressedRadio(var))
    profile_sel_1.grid(row=4, column=0)
    profile_sel_2.grid(row=4, column=1)
    profile_sel_3.grid(row=4, column=2)
    profile_sel_4.grid(row=4, column=3)
    ser_port_btn.grid(row=5, column=3)

    tut.mainloop()

tutorial()

然而,当我使用完全相同的代码插入多窗口应用程序时,我从帮助菜单调用教程(见下文),Radiobutton的var参数不再改变。我刚开始编程,所以也许我在代码中有一个明显的错误。我将不胜感激任何帮助。谢谢。 附:我不知道它是否重要,但我在Mac上使用Pycharm

import tkinter as tk
from tkinter import *
from tkinter import ttk

def PressedRadio(var):
    selection = var.get()
    print('Pushed the button!')
    print('var has value', selection)
    text_dict = {0: 'Nothing selected!', 1: 'Profile 1 selected', 2: 'Profile 2 selected', 3: 'Profile 3 selected',
                 4: 'profile 4 selected'}
    text_to_print = text_dict[selection]
    print(text_to_print)

def tutorial():

    tut = tk.Tk()
    tut.wm_title("Tutorial")
    label=ttk.Label(tut, text="What do you need help with?")

    var = IntVar()
    profile_sel_1 = ttk.Radiobutton(tut, text='Choice 1', variable=var, value=1)
    profile_sel_2 = ttk.Radiobutton(tut, text='Choice 2', variable=var, value=2)
    profile_sel_3 = ttk.Radiobutton(tut, text='Choice 3', variable=var, value=3)
    profile_sel_4 = ttk.Radiobutton(tut, text='Choice 4', variable=var, value=4)
    ser_port_btn = ttk.Button(tut, text='ENTER', command=lambda:PressedRadio(var))
    profile_sel_1.grid(row=4, column=0)
    profile_sel_2.grid(row=4, column=1)
    profile_sel_3.grid(row=4, column=2)
    profile_sel_4.grid(row=4, column=3)
    ser_port_btn.grid(row=5, column=3)

    tut.mainloop()

class Myapp(tk.Tk):
    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)
        tk.Tk.wm_title(self, "My app")

        container = tk.Frame(self)
        container.pack(side="top", fill="both", expand=True)
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)

        menubar = tk.Menu(container)
        filemenu = tk.Menu(menubar, tearoff=0)
        filemenu.add_command(label="Exit")
        menubar.add_cascade(label="File", menu=filemenu)

        helpmenu = tk.Menu(menubar, tearoff=0)
        helpmenu.add_command(label="Tutorial", command=tutorial)
        menubar.add_cascade(label="Help", menu=helpmenu)

        tk.Tk.config(self, menu=menubar)

        self.frames={}
        for F in (StartPage, PageOne):
            frame=F(container, self)
            self.frames[F]=frame
            frame.grid(row=0, column=0, sticky="nsew")
        self.show_frame(StartPage)

    def show_frame(self, cont):
        frame=self.frames[cont]
        frame.tkraise()

class StartPage(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        label = ttk.Label(self, text="Page One")
        label.pack()

        button1 = ttk.Button(self, text="Go to page 1", command=lambda: controller.show_frame(PageOne))
        button1.pack()

class PageOne(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        label = ttk.Label(self, text="Page One")
        label.pack()

        button1 = ttk.Button(self, text="Back to Home", command=lambda: controller.show_frame(StartPage))
        button1.pack()

app=Myapp()
app.mainloop()

1 个答案:

答案 0 :(得分:2)

正如@jasonharper所说,主要的错误是在程序中两次调用Tk()。在使用Tk()时,调用tkinter初始化在场景后面运行的tcl / tk引擎,并且运行两个不同的引擎会发生奇怪的事情。

但我还建议另一个修改:在你当前的代码中,用户可以根据需要多次点击教程,启动任意数量的弹出窗口,这是很少需要的。因此,您可以将弹出窗口转换为模态窗口(请参阅here)以避免此行为。这是修改后的代码:

def tutorial():

    tut = tk.Toplevel()            # instead of tut = tk.Tk()
    tut.wm_title("Tutorial")

    ... same as previously ...

    tut.grab_set()                 # define popup as foreground window
    tut.wait_window()              # instead of tut.mainloop()