Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在Tkinter中从框架类中提升框架类_Python_Oop_Tkinter - Fatal编程技术网

Python 如何在Tkinter中从框架类中提升框架类

Python 如何在Tkinter中从框架类中提升框架类,python,oop,tkinter,Python,Oop,Tkinter,在第一个帧从根类中提升之后,我一直试图从第一个帧类中提升第二个帧,但每次都会出错 类型对象“root”没有属性“tk”。 但是我可以从根类中提升我的第二个框架,但这不是我想要的。我希望从上一帧提升每个连续帧,因为我希望在整个程序中有条件多次提升4帧。如果我的想法是错误的,那么请提供一个解决方法,下面是我的代码 import mysql.connector as sql from os import getenv from datetime import date from ttkthemes i

在第一个帧从根类中提升之后,我一直试图从第一个帧类中提升第二个帧,但每次都会出错 类型对象“root”没有属性“tk”。 但是我可以从根类中提升我的第二个框架,但这不是我想要的。我希望从上一帧提升每个连续帧,因为我希望在整个程序中有条件多次提升4帧。如果我的想法是错误的,那么请提供一个解决方法,下面是我的代码

import mysql.connector as sql
from os import getenv
from datetime import date
from ttkthemes import themed_tk as ttkt
from tkinter import ttk
import tkinter as tk
from tkinter import messagebox as mg

class root(ttkt.ThemedTk):
    def __init__(self, *args):
        ttkt.ThemedTk.__init__(self, *args)
        self.geometry('800x450')
        self.resizable(0, 0)
        self.set_theme('arc') #equilux
        self.config(background = '#EFEFEF')
        self.navbar()
        self.show_frame(home_first) 
#### I'm able to call Class_second frame from changing the value here

    def show_frame(self, cont):
        frame = cont(self)
        frame.pack(side = 'right', expand = True, fill= 'both', ipadx = 210)
        frame.tkraise()

    def show_Class_frame(self):
        self.show_frame(self, Class_second)

    def about(self):
    ....

    def session(self): 
    ....

    #NAVBAR______________________________
    def navbar(self):
        fl = tk.Frame(self, background = '#0077CC')
        fl.pack(side = 'left', expand = 1, fill= 'both')
        #NAVBAR's internal items______________________________
        ....

class home_first(tk.Frame):
    def __init__(self, parent):
        tk.Frame.__init__(self, parent)
        ....
        btnt = ttk.Button(btnfrm, text = 'Teachers', style = 'fntb.TButton', command = self.tch_sel)
        # I want to use these to get to second frame
        btnt.pack(pady = (0,8))
        btns = ttk.Button(btnfrm, text = 'Students', style = 'fntb.TButton', command = self.std_sel)
        btns.pack()

    def tch_sel(self):
        sel = 0 #for teachers direct to page 3 not Class_second Frame
        root.show_Class_frame(root)

    def std_sel(self):
        sel = 1 #for students move to Class_second frame
        root.show_Class_frame(root)

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

class vote_third(tk.Frame):
    pass #from here i need to bring 4th frame

class done_fourth(tk.Frame):
    pass #here there will be like 3 button each going either to Home, Class or Vote frames
if __name__ == '__main__':
    app = root()
    app.mainloop()
回溯

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\Sagar\Anaconda3\lib\tkinter\__init__.py", line 1705, in __call__
    return self.func(*args)
  File "d:\Code\Python\py_project_votm\vm_vot_main_oop.py", line 76, in tch_sel
    root.show_Class_frame(root)
  File "d:\Code\Python\py_project_votm\vm_vot_main_oop.py", line 31, in show_Class_frame
    self.show_frame(self, Class_second)
  File "d:\Code\Python\py_project_votm\vm_vot_main_oop.py", line 26, in show_frame
    frame = cont(self)
  File "d:\Code\Python\py_project_votm\vm_vot_main_oop.py", line 84, in __init__
    tk.Frame.__init__(self, parent)
  File "C:\Users\Sagar\Anaconda3\lib\tkinter\__init__.py", line 2744, in __init__
    Widget.__init__(self, master, 'frame', cnf, {}, extra)
  File "C:\Users\Sagar\Anaconda3\lib\tkinter\__init__.py", line 2292, in __init__
    BaseWidget._setup(self, master, cnf)
  File "C:\Users\Sagar\Anaconda3\lib\tkinter\__init__.py", line 2262, in _setup
    self.tk = master.tk
AttributeError: type object 'root' has no attribute 'tk'
还有,有人能告诉我还有什么其他的方法更适合做这个项目吗。
对不起,英语不好

部分问题在于您没有使用命名约定,这使得您的代码难以阅读和理解。当我们阅读代码时,我们会根据命名约定做出假设,如果不使用命名约定,我们最终会做出错误的假设

具体来说,您的所有课程都需要以大写字母开头(
Root
Vote\u third
Done\u fourth
)。当我们阅读代码并看到类似于
root.some\u method()
的内容时,我们假设您正在对实例调用
some\u method()
。然而,在你的例子中,你是在类本身上调用它

问题在于这一行:

root.show_Class_frame(root)
root
是一个类,而不是类的实例。此参数最终成为另一个小部件的主控项,但不能将类用作主控项。您必须使用小部件类的实例作为主类

您已经在创建名为
app
root
实例。不要调用
root.show\u Class\u frame()
你需要调用
app.show\u Class\u frame

def tch_sel(self):
    sel = 0 #for teachers direct to page 3 not Class_second Frame
    app.show_Class_frame()

def std_sel(self):
    sel = 1 #for students move to Class_second frame
    app.show_Class_frame()

此外,如注释中所述,
self.show\u frame(self,Class\u second)
需要更改为
self.show\u frame(Class\u second)

根据,您必须使用ttkthemes import-ThemedTk中的
,然后使用
类根(ThemedTk):;定义初始值(自):;超级()。请看,我自己无法在程序中实现@stovfl中提供的方法,主要是因为我是oop新手(这是我在oop中的第二个程序),如果其他人能够实现它并发布代码,那将很有帮助。“如果其他人能够实现它并发布代码,那将很有帮助。”-这不是stackoverflow的目的。不管问题解决了^ ^好吧,通过以上答案,所有问题都解决了。更改-使用
Home\u优先。打包忘记(self);app.show_frame(Class_second)
和删除的
show_Class_frame()
方法(不需要它)就是这样。