我在一个名为views1.py 的模块中有一个菜单栏,我正在使用OOP 将它导入到主模块中。我没有收到错误消息,也没有菜单栏。该窗口会正常打开并加载其他内容。该应用程序正在作为一个包开发,目录结构如下:
Main 目录有两个文件 main.py 和 README.rst 以及一个名为小写main 的目录。此目录小写main 包含 views1.py、controller.py 和 model.py。
1. Main directory
- main.py
- README.rst
- main directory
* views1.py
* model.py
* controller.py
主文件
from tkinter import Tk
import tkinter as tk
import controller
import views1
from tkinter import *
from views1 import Views1, Views2
class AIcvmaker(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
self.Views2 = views1.Views2(tk)
self.title("Cleopatra CV Maker")
self.geometry("1400x800")
#Add a view
Views2(self)
Views1(self).grid(sticky = (tk.E))
self.columnconfigure(0, weight=1)
root = AIcvmaker()
root.mainloop()
意见1.py
from tkinter import Tk
import tkinter as tk
from tkinter import *
from tkinter import messagebox
import controller
#code edited for Views1()
class Views1(tk.Frame):
def __init__(self, parent, *args, **kwargs):
super().__init__(parent, *args, **kwargs)
self.controller = controller
self.parent = parent
self.cv_maker_Base()
def cv_maker_Base(self):
self.create_Rightframe()
def create_Rightframe(self):
self.pad_label = ttk.Label(self, text = "Push a button")
self.activate_Button = ttk.Button(self, text = "Activate software")
self.pad_label.grid(row=0, column=1, padx = 8, pady = 8)
self.activate_Button.grid(row=1, column=1, padx = 8, pady = 8)
self.columnconfigure(1, weight = 1)
#ADDING AN EXTRA self to super()
class Views2(tk.Menu):
def __init__(self, parent):
super().__init__(self, parent)
self.parent = parent
self.create_Topmenu()
def create_Topmenu(self):
self.menu_bar = Menu(self.parent)
self.create_Filemenu()
self.create_Editmenu()
self.create_Viewmenu()
self.create_Aboutmenu()
def create_Filemenu(self):
self.filemenu = Menu(self.menu_bar, tearoff = 0)
self.filemenu.add_command(label="New", command = lambda: fNew())
self.filemenu.add_command(label="Open", command= lambda: fOpen())
self.filemenu.add_separator()
self.filemenu.add_command(label="Save", command= lambda: fSave())
self.filemenu.add_command(label="Exit", command=quit)
self.menu_bar.add_cascade(label="File", menu=self.filemenu)
def create_Editmenu(self):
pass
def create_Viewmenu(self):
pass
def create_Aboutmenu(self):
pass
def fNew(self):
pass
def fOpen(self):
pass
def fSave(self):
pass
我尝试在 Views2() 的 init 方法中编写 tk.Menu 并尝试将 self 更改为 parent,它似乎不起作用。我正在编写一个 MVC,因为代码非常长,这将有助于降低复杂性。