0

我一直拒绝发布这个问题,但最终决定把我的骄傲放在口袋里,承认我无法让我的程序的菜单创建部分工作。我似乎仍然无法完全掌握 Classes 的工作原理。我编写的程序基本上是一个现金簿,用于跟踪现金收入和支出,并将其分析到各个账户。这一切都很好。然后我还添加了第二个窗口来添加分析项目。这也很好用。但是,最后我尝试添加菜单但无法使其正常工作。当我运行程序时,它不会抛出任何错误,但不会出现菜单(“文件/退出”)。有人可以看看下面的(Menu_ex)代码并告诉我它在哪里失败了吗?谢谢你的期待。

import tkinter as tk
from tkinter import ttk
from tkinter import Tk, Frame, Menu
from tkcalendar import Calendar, DateEntry
import sqlite3
import datetime as dt
import time
from tkinter import messagebox as msgbox
from prettytable import PrettyTable
from prettytable import MSWORD_FRIENDLY
import subprocess, sys

class Window(tk.Toplevel):
    def __init__(self, parent):
        super().__init__(parent)

        self.geometry('450x200')
        self.title('Add a new Analysis Item')


        def Item_sel(): 
            """ Set the value of the selected Radio buttons"""

            global item_choice
            global Item_Cat

            item_choice=Item_radio_var.get()

            
            if item_choice==1:


                Item_Analyse.config(state='normal')
                print('Item_radio_var = ' + str(item_choice))
                Item_Cat='IncCat'
                print('The item selected is ' + Item_Cat)
                Item_Analyse.focus()
                
            elif item_choice==2:
                #Item_Analyse.delete(0,tk.END)
                Item_Analyse.config(state='normal')
                print('Item_radio_var = ' + str(item_choice))
                Item_Cat='ExpCat'
                print('The item selected is ' + Item_Cat)
                Item_Analyse.focus()

class Menu_ex():

    def __init__(self,parent):    
        super().__init__(parent)

        #self.initUI()

    def initUI(self):

        menubar=Menu(self.parent)
        self.parent.config(menu=menubar)

        fileMenu=Menu(menubar)
        fileMenu.add_command(label='Exit', command=self.onExit)
        menubar.add_cascade(label='File',menu=fileMenu)

    def onExit(self):

        self.quit()

class MainWindow(tk.Tk):

    def __init__(self):    
        super().__init__()
        
        

        global format_date
        date = dt.datetime.now()
        format_date=f"{date:%Y-%m-%d}"
        print(format_date)
        self.title('My Nedbank Credit Card')
        self.geometry('1130x450')
        self.grid_rowconfigure(0,weight=1)
        self.grid_columnconfigure(0,weight=1)
        self.resizable(width=False,height=False)
     

        """Create a Frame inside a Window to contain widgets"""

        frame1 = ttk.Frame(self)
        frame1.grid(column=0,row=0, pady=5, sticky='news')

        """Create widgets in Frame1"""

        l_Sub = tk.Label(frame1,text='Nedbank Credit Card', fg='blue')
        l_Sub.config(font=('Courier Bold', 16))
        l_Sub.place(relx=.05,rely=.5)
        l_Sub.grid(row=0, column=0, pady=10,sticky='news',columnspan=4)

        l_SubHead = tk.Label(frame1,text='Enter transaction data', fg='blue')
        l_SubHead.config(font=('Courier Bold', 12))
        l_SubHead.place(relx=.05,rely=.5)
        l_SubHead.grid(row=1, column=0, pady=10,sticky='news',columnspan=4)

def open_window(self):
        window = Window(self)
        window.grab_set()

if __name__ == "__main__":
    app = MainWindow()
    app.mainloop()
4

1 回答 1

0

简短的回答是您实际上并没有创建 Menu 类的实例,因此它永远不会显示

下面的代码显示菜单并修复退出按钮

import tkinter as tk
from tkinter import ttk
from tkinter import Tk, Frame, Menu
#from tkcalendar import Calendar, DateEntry
import sqlite3
import datetime as dt
import time
from tkinter import messagebox as msgbox
#from prettytable import PrettyTable
#from prettytable import MSWORD_FRIENDLY
import subprocess, sys

class Window(tk.Toplevel):
    def __init__(self, parent):
        super().__init__(parent)

        self.geometry('450x200')
        self.title('Add a new Analysis Item')


        def Item_sel(): 
            """ Set the value of the selected Radio buttons"""

            global item_choice
            global Item_Cat

            item_choice=Item_radio_var.get()

            
            if item_choice==1:


                Item_Analyse.config(state='normal')
                print('Item_radio_var = ' + str(item_choice))
                Item_Cat='IncCat'
                print('The item selected is ' + Item_Cat)
                Item_Analyse.focus()
                
            elif item_choice==2:
                #Item_Analyse.delete(0,tk.END)
                Item_Analyse.config(state='normal')
                print('Item_radio_var = ' + str(item_choice))
                Item_Cat='ExpCat'
                print('The item selected is ' + Item_Cat)
                Item_Analyse.focus()

class Menu_ex():

    def __init__(self,parent):    
        self.parent = parent
        self.initUI()

    def initUI(self):

        self.menubar=Menu(self.parent)
        self.parent.config(menu=self.menubar)

        fileMenu=Menu(self.menubar)
        fileMenu.add_command(label='Exit', command=self.onExit)
        self.menubar.add_cascade(label='File',menu=fileMenu)

    def onExit(self):

        self.parent.destroy()

class MainWindow(tk.Tk):

    def __init__(self):    
        super().__init__()
        
        

        global format_date
        date = dt.datetime.now()
        format_date=f"{date:%Y-%m-%d}"
        print(format_date)
        self.title('My Nedbank Credit Card')
        self.geometry('1130x450')
        self.grid_rowconfigure(0,weight=1)
        self.grid_columnconfigure(0,weight=1)
        self.resizable(width=False,height=False)
        self.menu = Menu_ex(self)
     

        """Create a Frame inside a Window to contain widgets"""

        frame1 = ttk.Frame(self)
        frame1.grid(column=0,row=0, pady=5, sticky='news')

        """Create widgets in Frame1"""

        l_Sub = tk.Label(frame1,text='Nedbank Credit Card', fg='blue')
        l_Sub.config(font=('Courier Bold', 16))
        l_Sub.place(relx=.05,rely=.5)
        l_Sub.grid(row=0, column=0, pady=10,sticky='news',columnspan=4)

        l_SubHead = tk.Label(frame1,text='Enter transaction data', fg='blue')
        l_SubHead.config(font=('Courier Bold', 12))
        l_SubHead.place(relx=.05,rely=.5)
        l_SubHead.grid(row=1, column=0, pady=10,sticky='news',columnspan=4)

def open_window(self):
        window = Window(self)
        window.grab_set()

if __name__ == "__main__":
    app = MainWindow()
    app.mainloop()
于 2021-10-25T10:12:02.297 回答