简体   繁体   English

在Tkinter中重叠不同的帧

[英]Overlap different frames in Tkinter

I'm trying to make a calculator and it has different frames ie Basic, Statistics, ..., etc. However, I'm having issues to show each frame. 我正在尝试制作一个计算器,它具有不同的框架,例如基本,统计,...等。但是,在显示每个框架时遇到了问题。

This is the container for all the frames (I took a code of a previous post as example) 这是所有框架的容器(我以上一篇文章的代码为例)

import tkinter as tk

class calculatorframe(tk.Tk):
    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)
        #----------------CONTAINER FOR ALL THE FRAMES----------
        container = tk.Frame(self)
        container.pack()
        #--------------------- DROPDOWN MENU------------------

        tkvar = tk.StringVar()
        choices={'Basic Mode','Calculus Mode'} #Options of the dropdown menu
        tkvar.set('Basic Mode') #default frame
        dropdownmenu =tk.OptionMenu(container, tkvar, *choices)
        dropdownmenu.grid(row=2,column=3) #position of the dropdown menu

        self.frames = {}


        for F in (Basic, Calculus):
            page_name = F.__name__
            frame = F(parent= container, controller=self)
            self.frames[page_name] = frame

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

        self.show_frame('Basic')

    #-------FUNCTION TO SHOW THE CURRENT FRAME

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

These are the classes that I created for the basic calculator 这些是我为基本计算器创建的类

class Basic(tk.Frame):

    def __init__(self, parent, controller):
        #--------------- SCREEN ---------------
        tk.Frame.__init__(self, parent)
        screen = tk.Entry(self, width=80)  
        screen.grid(row=3, column=1,columnspan=7) #position of the screen
        #------------ BUTTON ---------------------
        button7=tk.Button(self, text="7", width=8)  #button
        button7.grid(row=4,column=1)
#---------------------frame for calculus -------------------------
class Calculus(tk.Frame):
    def __init__(self, parent, controller):
            tk.Frame.__init__(self, parent)
            #-----------------------------SCREEN------------
            screen=tk.Entry(self, width=50)
            screen.pack()
            screen.grid(row=3, column=1, pady=20, columnspan=7) #position of the screen
            #------------------------BUTTON---------------
            go=tk.Button(self, height=1, text="Go") #button
            go.grid(row=1, column=8)


if __name__ == "__main__":
    app = calculatorframe()
    app.mainloop()

I'm aware that I have to keep track of the value of tkvar and that I need to do it using trace() and pass that value to show_frame, however, I don't know where to place it in my code. 我知道我必须跟踪tkvar的值,并且需要使用trace()并将其传递给show_frame,但是,我不知道将其放置在代码中的哪个位置。 I tried to put it below the dropdown menu, but I get an error message and I tried after the function show_frame and it did not work either. 我尝试将其放在下拉菜单下方,但收到一条错误消息,并在函数show_frame之后尝试,但它也无法正常工作。 I'm a bit stuck, I would really appreciate your help, thanks in advance. 我有点受阻,在此先感谢您的帮助。

The simple solution would be to add a command to your OptionsMenu() function. 简单的解决方案是将命令添加到OptionsMenu()函数。 We will also need to change your class names and your choice options due to how the command argument works here. 由于命令参数在这里的工作方式,我们还需要更改您的类名和选择选项。

For the OptionsMenu() command argument when you tell it to call a method it will automatically pass the value of the selected item in the drop down. 对于OptionsMenu()命令参数,当您告诉它调用方法时,它将自动在下拉列表中传递所选项目的值。 So because of this we need to make sure our selection reflect the class names. 因此,因此,我们需要确保我们的选择反映出类名。 You can change the choices/classes to be whatever you wish I just used BasicMode and CalculusMode as an example. 您可以将选择/类更改为您希望的任何类型,我只是以BasicModeCalculusMode为例。

The command will automatically pass the value selected so we can use that to call each frame using you show_frame method. 该命令将自动传递所选的值,因此我们可以使用show_frame方法使用该值来调用每个帧。

Take a look at the below code and let me know if you have any questions. 请看下面的代码,如果您有任何疑问,请告诉我。

import tkinter as tk


class calculatorframe(tk.Tk):
    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)
        container = tk.Frame(self)
        container.pack()

        tkvar = tk.StringVar()
        choices = {'BasicMode', 'CalculusMode'}
        tkvar.set('BasicMode')
        dropdownmenu = tk.OptionMenu(container, tkvar, *choices, command=self.show_frame)
        dropdownmenu.grid(row=2, column=3)

        self.frames = {}

        for F in (BasicMode, CalculusMode):
            page_name = F.__name__
            frame = F(parent= container, controller=self)
            self.frames[page_name] = frame

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

        self.show_frame('BasicMode')

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


class BasicMode(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        screen = tk.Entry(self, width=80)  
        screen.grid(row=3, column=1, columnspan=7)

        button7 = tk.Button(self, text="7", width=8)
        button7.grid(row=4,column=1)


class CalculusMode(tk.Frame):
    def __init__(self, parent, controller):
            tk.Frame.__init__(self, parent)
            screen = tk.Entry(self, width=50)
            screen.pack()
            screen.grid(row=3, column=1, pady=20, columnspan=7)
            go = tk.Button(self, height=1, text="Go")
            go.grid(row=1, column=8)


if __name__ == "__main__":
    app = calculatorframe()
    app.mainloop()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM