根据单选按钮应答更改帧

时间:2016-04-02 19:17:27

标签: python tkinter

我正在尝试一个简单的实验,我有3个帧,第1帧有两个标签 - "第2页"和#34;对于第3页",它还有2个与标签对应的单选按钮。根据选择的单选按钮,当用户点击下一页按钮时,我希望按钮将用户带到所选页面

这是代码 -

import Tkinter as tk

class MainApp(tk.Tk):

    def __init__(self, *args, **kwargs):

        tk.Tk.__init__(self, *args, **kwargs)

        # the main container that holds all the frames
        container = tk.Frame(self)

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


        self.frames = {}

        # adding frames to the dictionary
        for F in (Page1,Page2,Page3):

             frame = F(container,self)

             self.frames[F] = frame

             frame.grid(row = 0, column = 0, sticky = "w")

        self.show_frame(Page1)

    def show_frame(self,page_name):

        #SHOWS A FRAME WITH THE GIVEN NAME
        for frame in self.frames.values():
            frame.grid_remove()
        frame = self.frames[page_name]
        frame.grid()

    #STACKING THE FRAMES
    #frame = self.frames[cont]
    #frame.tkraise()

class Page1(tk.Frame):
    def __init__(self,parent,controller):
        tk.Frame.__init__(self,parent)

        lbl1 = tk.Label(self,text = "for page 2",font =("Helvetica",12,"bold"))
        lbl1.grid(row=1,sticky="W")

        lbl2 = tk.Label(self,text = "for page 3",font =("Helvetica",12,"bold"))
        lbl2.grid(row=1,column=1,sticky="W")

        btn1 = tk.Button(self, text="next page", font=('MS', 24, 'bold'))
        btn1.grid(row=3,column = 0,columnspan=1)
        #btn1['command'] = lambda: controller.show_frame(Page2)


        self.var1 = tk.BooleanVar()

        rButton1 = tk.Radiobutton(self,variable = self.var1,value=True)
        rButton1.grid(row=2,sticky = "W")

        rButton2 = tk.Radiobutton(self,variable = self.var1,value=False)
        rButton2.grid(row=2,column=1,sticky = "W")

        if self.var1.get() == 1:
            btn1['command'] = lambda: controller.show_frame(Page3)

        if self.var1.get() == 0:
            btn1['command'] = lambda: controller.show_frame(Page2)


class Page2(tk.Frame):
    def __init__(self,parent,controller):
        tk.Frame.__init__(self,parent)

        lbl = tk.Label(self,text="This is page 2",font=("Helvetica",12,"bold"))
        lbl.pack()


class Page3(tk.Frame):
    def __init__(self,parent,controller):
        tk.Frame.__init__(self,parent)

        lbl = tk.Label(self,text="This is page 3",font=("Helvetica",12,"bold"))
        lbl.pack()

app = MainApp()
app.mainloop()

我认为通过使用一些基本条件(位于我的PageOne类中) -

        self.var1 = tk.BooleanVar()

    rButton1 = tk.Radiobutton(self,variable = self.var1,value=True)
    rButton1.grid(row=2,sticky = "W")

    rButton2 = tk.Radiobutton(self,variable = self.var1,value=False)
    rButton2.grid(row=2,column=1,sticky = "W")

    if self.var1.get() == 1:
        btn1['command'] = lambda: controller.show_frame(Page3)

    if self.var1.get() == 0:
        btn1['command'] = lambda: controller.show_frame(Page2)

我能够做到这一点,但它似乎无法奏效。我的if语句中的条件是整数,但据我所知1代表True和0;无论如何都是假的?我做错了什么?

1 个答案:

答案 0 :(得分:0)

我认为这就是你想要的。我没有处理确保默认情况下未选择单选按钮。我把它作为锻炼留给你了。虽然,如果您只想切换这样的页面我只使用按钮(tk/ttk.Button),那么您不必担心处理无线电按钮。虽然,这只是我的偏好当然也可以正常工作。您只需绑定每个按钮即可切换页面。我在下面的修改后的代码中注释了按钮。 如果您想要为每个页面创建按钮/单选按钮以具有前进/后退选项。您可以迭代控制器帧以查看哪个是当前的,并创建两个类似于下面的按钮以移动到其他帧。

import tkinter as tk

class MainApp(tk.Tk):

    def __init__(self, *args, **kwargs):

        tk.Tk.__init__(self, *args, **kwargs)

        # the main container that holds all the frames
        container = tk.Frame(self)

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


        self.frames = {}

        # adding frames to the dictionary
        for F in (Page1,Page2,Page3):

             frame = F(container,self)

             self.frames[F] = frame

             frame.grid(row = 0, column = 0, sticky = "w")

        self.show_frame(Page1)

    def show_frame(self,page_name):

        #SHOWS A FRAME WITH THE GIVEN NAME
        for frame in self.frames.values():
            frame.grid_remove()
        frame = self.frames[page_name]
        frame.grid()

    #STACKING THE FRAMES
    #frame = self.frames[cont]
    #frame.tkraise()

class Page1(tk.Frame):
    def __init__(self,parent,controller):
        tk.Frame.__init__(self,parent)
        self.controller = controller

        lbl1 = tk.Label(self,text = "for page 2",font =("Helvetica",12,"bold"))
        lbl1.grid(row=1,sticky="W")

        lbl2 = tk.Label(self,text = "for page 3",font =("Helvetica",12,"bold"))
        lbl2.grid(row=1,column=1,sticky="W")

        btn1 = tk.Button(self, text="next page", font=('MS', 24, 'bold'))
        btn1.grid(row=3,column = 0,columnspan=1)
        #btn1['command'] = lambda: controller.show_frame(Page2)


        self.var1 = tk.BooleanVar()

        #rButton1 = tk.Button(self, text='Show Page 2', command=lambda: self.controller.show_frame(Page2))
        #rButton1.grid(row=2, sticky="W")
        #rButton2 = tk.Button(self, text='Show Page 3', command=lambda: self.controller.show_frame(Page3))
        #rButton2.grid(row=2, column=1, sticky="W")

        rButton1 = tk.Radiobutton(self,variable = self.var1,value=True,
            command=self.switch_pages)
        rButton1.grid(row=2,sticky = "W")

        rButton2 = tk.Radiobutton(self,variable = self.var1,value=False,
            command=self.switch_pages)
        rButton2.grid(row=2,column=1,sticky = "W")

    def switch_pages(self):

        if not self.var1.get():
            self.controller.show_frame(Page3)
        else:
            self.controller.show_frame(Page2)


class Page2(tk.Frame):
    def __init__(self,parent,controller):
        tk.Frame.__init__(self,parent)

        lbl = tk.Label(self,text="This is page 2",font=("Helvetica",12,"bold"))
        lbl.pack()


class Page3(tk.Frame):
    def __init__(self,parent,controller):
        tk.Frame.__init__(self,parent)

        lbl = tk.Label(self,text="This is page 3",font=("Helvetica",12,"bold"))
        lbl.pack()

app = MainApp()
app.mainloop()