簡體   English   中英

單擊主類中的按鈕時,如何運行整個子類

[英]How can i run a whole sub class when the buttons are clicked which are in the main class

這是我第一次嘗試子類,所以我不知道我是否做得對,但是當我運行它時,我得到了一些錯誤

File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.1520.0_x64__qbz5n2kfra8p0\lib\tkinter\__init__.py", line 1892, in __call__
        return self.func(*args)
    TypeError: cart_order() missing 1 required positional argument: 'self'

File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.1520.0_x64__qbz5n2kfra8p0\lib\tkinter\__init__.py", line 1892, in __call__
    return self.func(*args)
  File "C:\Users\GGPC\OneDrive\Desktop\trying subclasses.py", line 337, in order_menu
    t = final_cart(parent, Test)
NameError: name 'parent' is not defined.

當我使用command=t.calculate_order我得到了錯誤

    self.button_checkout = Button(self.my_frame2, command = t.cart_order)
NameError: name 't' is not defined

單擊主類中的按鈕時,嘗試運行子類“final_cart”。 我做了很多測試和試用,但找不到解決方案,因此感謝您的幫助。

謝謝你。

##importing all the models needed to create the program
from tkinter import*
from PIL import Image, ImageTk
import tkinter as ttk
import os


accounts = []
food = ['Pizza','Burger','Nachos', 'French Toast']
drinks = ['Pepsi','Lemonade','Tea', 'Aperitivo Spritz']
foodprice=['20','9.50','7.50', '17']
drinksprice = ['3','4','3', '15.50']
orderlist = []



class Goode_brothers:




    def __init__(self, parent):

        
        self.my_frame = Frame(parent)
        self.my_frame.pack()

        
        self.background = Image.open('new-dip-project\\food.jpg')
        self.background_image = ImageTk.PhotoImage(self.background)
        self.img = Label(parent, image = self.background_image)
        self.img.place(x = -26, y =0)

        
        self.img_login = PhotoImage(file = 'new-dip-project\\button (3).png')
        self.login_button = Button(parent,image = self.img_login, command = self.read_info, bd = 0, cursor = "hand2", bg = '#3b353b', activebackground = '#3b353b')
        self.login_button.place(x = 275, y = 340)

        

        
        self.email = Entry(parent)
        self.email.place(x = 340, y = 180)

        
        self.password = Entry(parent, show = '●')
        self.password.place(x = 354, y = 250)

        
       



    def save_info(self):

         self.email_reg = str(self.email2.get())
         self.pass_word = str(self.password2.get())
         print(self.email2)
         file = open('emails.txt', 'a+')
         file.write(self.email_reg + ', ' + self.pass_word + '\n')


    def read_info(self):

        with open("emails.txt") as read_ep:
            for line in read_ep:
                ##appending the details in a list
                accounts.append(line.strip().split(", "))
        ## getting what the user entered for email and password
        credential = [self.email.get(), self.password.get()]
        ## if what the user entered is in the list then user can continue
        if credential in accounts:
            self.open_menu()
        else:
            ## if the above conditions are not satisfied then user has th=o retry and this function will repeat until logged in
            self.ep_notexist = Label(root, text = "Your Email or Password is incorrect, Please try again", font=("open sans", "8"))
            self.ep_notexist.place(x = 210, y = 300)
            self.ep_notexist.after(4000, self.ep_notexist.destroy)
            self.email.delete(0, END)
            self.password.delete(0, END)


    ## creating a function for home page
    def open_menu(self):

       
        for wid in root.winfo_children():
            wid.destroy()
       
        self.my_frame.destroy()
        self.my_frame2 = Frame(root)
        self.my_frame2.pack(fill = "both", expand = 1)

        self.title_home = PhotoImage(file = 'new-dip-project\\goode.png')
        self.title2 = Label(self.my_frame2, image = self.title_home).pack()

        self.img_menu = PhotoImage(file = 'new-dip-project\\menu_button.png')
        self.button_menu = Button(self.my_frame2,image = self.img_menu, command = self.view_menu, cursor = "hand2", bd  = 0)
        self.button_menu.place(x = 246, y = 140)

        self.img_order = PhotoImage(file = 'new-dip-project\\order_button.png')
        self.button_order = Button(self.my_frame2,image = self.img_order, command = self.order_menu, cursor = "hand2", bd  = 0)
        self.button_order.place(x = 239, y = 228)

        self.img_checkout = PhotoImage(file = 'new-dip-project\\checkout.png')
        self.button_checkout = Button(self.my_frame2,image = self.img_checkout, cursor = "hand2", bd  = 0, command = final_cart.cart_order)
        self.button_checkout.place(x = 250, y = 316)


    def view_menu(self):

        self.my_frame2.pack_forget()
        self.my_frame3 = LabelFrame(root, height = 700)
        self.my_frame3.pack()

        self.my_frame3.columnconfigure(0, weight=1)
        self.my_frame3.columnconfigure(1, weight=2)

        self.food_title = Label(self.my_frame3, font=("Impact", "23"), text = 'Food').grid(row = 0, column = 4)
        self.food_space = Label(self.my_frame3, text = '').grid(row = 1, column = 4)
        self.drinks_title = Label(self.my_frame3, font=("Impact", "23"), text = 'Drinks').grid(row = 8, column = 4)
        self.price = Label(self.my_frame3, font=("Impact", "23"), text = 'Price($)').grid(row = 0, column = 8)

        for x in range (len(food)):
            self.foodop = Label(self.my_frame3, font=("Impact", "15"), text = food[x]).grid(row = 3+x, column = 4)
            self.fprice = Label(self.my_frame3, font=("Impact", "15"), text = foodprice[x]).grid(row = 3+x, column = 8)

        for x in range (len(drinks)):
            self.drinksop = Label(self.my_frame3, font=("Impact", "15"), text = drinks[x]).grid(row = 5+(len(food))+x, column = 4)
            self.drinksp = Label(self.my_frame3, font=("Impact", "15"), text = drinksprice[x]).grid(row = 5+(len(food))+x, column = 8)

        self.img_back = PhotoImage(file = 'new-dip-project\\back_button.png')
        self.back_button = Button(self.my_frame3,image = self.img_back, command = self.open_menu, cursor = "hand2", bd  = 0)
        self.back_button.grid(row = 38, column = 7)





    def order_menu(self):


        self.my_frame2.destroy()
        self.my_frame4 = Frame(root)
        self.my_frame4.pack(fill = "both", expand = 1)


        self.tkvar = StringVar(self.my_frame4)
        self.tkvar.set("Food")
        self.tkvar.trace_add('write', lambda *args: print(self.tkvar.get()))

        self.tkvar2 = StringVar(self.my_frame4)
        self.tkvar2.set("Drinks")
        self.tkvar2.trace_add('write', lambda *args: print(self.tkvar2.get()))

        self.img_odmenu = PhotoImage(file = 'new-dip-project\\od_menu.png')
        self.order_menu_message = Label(self.my_frame4, image = self.img_odmenu).place(x = 220)

        self.foodMenu = OptionMenu(self.my_frame4, self.tkvar, *['Pizza','Burger','Nachos', 'French Toast'])
        self.foodMenu.place(x = 160, y = 110)



        self.drinkMenu = OptionMenu(self.my_frame4, self.tkvar2, *['Pepsi','Lemonade','Tea', 'Aperitivo Spritz'])
        self.drinkMenu.place(x = 385, y = 110)


        self.order_btn1 = PhotoImage(file = 'new-dip-project\\orderb.png')
        self.order_button2 = Button(self.my_frame4, text = "SHOW", image = self.order_btn1, command = t.calculate_order, cursor = "hand2", bd = 0)
        self.order_button2.place(x = 302, y = 160)

        self.check_btn = PhotoImage(file = 'new-dip-project\\checkpay.png')
        self.checkout_btn = Button(self.my_frame4, image = self.check_btn, cursor = "hand2", bd = 0, command = t.cart_order)
        self.checkout_btn.place(x = 267, y = 410)

        self.img_back3 = PhotoImage(file = 'new-dip-project\\bbutton.png')
        self.back_button3 = Button(self.my_frame4, image = self.img_back3, cursor = "hand2", bd = 0, command = self.open_menu)
        self.back_button3.place(x = 50, y = 410)

        t = final_cart(parent, Goode_brothers)




class final_cart(Goode_brothers):



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


    def cart_order(self):
        self.root3 = Toplevel(root)
        self.root3.geometry("600x400")

        self.img_back2 = PhotoImage(file = 'new-dip-project\\bbutton.png')
        self.back_button2 = Button(self.root3, image = self.img_back2, cursor = "hand2", bd = 0, command = self.open_menu)
        self.back_button2.grid(column=0, row = 1)

        self.img_cart = PhotoImage(file = 'new-dip-project\\your_cart.png')
        self.y_cart = Label(self.root3, image = self.img_cart).grid(column=3, row = 1)

        Label(self.root3, text = "",font=("Courier New","12")).grid(column = 1, row = 1)

        Label(self.root3, text = "",font=("Courier New","12")).grid(column = 1, row = 4)

        for x in range (len(orderlist)):
            Label(self.root3, text = (str(x+1)),font=("Courier New","12")).grid(column =0, row= x+5)
            Label(self.root3, text = (orderlist[x][0]), font=("Courier New","12")).grid(column=2, row = x+5)




    def calculate_order(self):
       
        print (orderlist)


    def food_optionmenu(self, *args):
        self.position = food.index(self.tkvar.get())

    def drink_optionmenu(self, *args):
        self.position2 = drinks.index(self.tkvar2.get())
        self.tkvar.trace('w', self.food_optionmenu)
        self.tkvar2.trace('w', self.drink_optionmenu)



## main routine
if __name__ == "__main__":
    root = Tk()
    root.geometry('670x466')
    FoodSystem = Goode_brothers(root)

    root.title('Goode brothers')
    root.mainloop()

好的,現在我可以通過你的方法看到路線了。 他們只需要清理一下。 我將添加一些: ##注釋以顯示我更改了某些內容的位置:

##importing all the models needed to create the program
from tkinter import*
from PIL import Image, ImageTk
import tkinter as ttk
import os


accounts = []
food = ['Pizza','Burger','Nachos', 'French Toast']
drinks = ['Pepsi','Lemonade','Tea', 'Aperitivo Spritz']
foodprice=['20','9.50','7.50', '17']
drinksprice = ['3','4','3', '15.50']
orderlist = []


class Goode_brothers:

    def __init__(self, parent):

        self.my_frame = Frame(parent)
        self.my_frame.pack()

        self.background = Image.open('new-dip-project\\food.jpg')
        self.background_image = ImageTk.PhotoImage(self.background)
        self.img = Label(parent, image = self.background_image)
        self.img.place(x = -26, y =0)

        
        self.img_login = PhotoImage(file = 'new-dip-project\\button (3).png')
        self.login_button = Button(parent,image = self.img_login, command = self.read_info, bd = 0, cursor = "hand2", bg = '#3b353b', activebackground = '#3b353b')
        self.login_button.place(x = 275, y = 340)

        
        self.email = Entry(parent)
        self.email.place(x = 340, y = 180)

        
        self.password = Entry(parent, show = '●')
        self.password.place(x = 354, y = 250)

        
    def save_info(self):

         self.email_reg = str(self.email2.get())
         self.pass_word = str(self.password2.get())
         print(self.email2)
         file = open('emails.txt', 'a+')
         file.write(self.email_reg + ', ' + self.pass_word + '\n')


    def read_info(self):

        with open("emails.txt") as read_ep:
            for line in read_ep:
                ##appending the details in a list
                accounts.append(line.strip().split(", "))
        ## getting what the user entered for email and password
        credential = [self.email.get(), self.password.get()]
        ## if what the user entered is in the list then user can continue
        if credential in accounts:
            self.open_menu()
        else:
            ## if the above conditions are not satisfied then user has th=o retry and this function will repeat until logged in
            self.ep_notexist = Label(root, text = "Your Email or Password is incorrect, Please try again", font=("open sans", "8"))
            self.ep_notexist.place(x = 210, y = 300)
            self.ep_notexist.after(4000, self.ep_notexist.destroy)
            self.email.delete(0, END)
            self.password.delete(0, END)


    ## creating a function for home page
    def open_menu(self):

        for wid in root.winfo_children():
            wid.destroy()
       
        self.my_frame.destroy()
        self.my_frame2 = Frame(root)
        self.my_frame2.pack(fill = "both", expand = 1)

        self.title_home = PhotoImage(file = 'new-dip-project\\goode.png')
        self.title2 = Label(self.my_frame2, image = self.title_home).pack()

        self.img_menu = PhotoImage(file = 'new-dip-project\\menu_button.png')
        self.button_menu = Button(self.my_frame2,image = self.img_menu, command = self.view_menu, cursor = "hand2", bd  = 0)
        self.button_menu.place(x = 246, y = 140)

        self.img_order = PhotoImage(file = 'new-dip-project\\order_button.png')
        self.button_order = Button(self.my_frame2,image = self.img_order, command = self.order_menu, cursor = "hand2", bd  = 0)
        self.button_order.place(x = 239, y = 228)

        self.img_checkout = PhotoImage(file = 'new-dip-project\\checkout.png')
        self.button_checkout = Button(self.my_frame2,image = self.img_checkout, cursor = "hand2", bd  = 0, command = self.cart_order)  ## just self.cart_order
        self.button_checkout.place(x = 250, y = 316)


    def view_menu(self):

        self.my_frame2.pack_forget()
        self.my_frame3 = LabelFrame(root, height = 700)
        self.my_frame3.pack()

        self.my_frame3.columnconfigure(0, weight=1)
        self.my_frame3.columnconfigure(1, weight=2)

        self.food_title = Label(self.my_frame3, font=("Impact", "23"), text = 'Food').grid(row = 0, column = 4)
        self.food_space = Label(self.my_frame3, text = '').grid(row = 1, column = 4)
        self.drinks_title = Label(self.my_frame3, font=("Impact", "23"), text = 'Drinks').grid(row = 8, column = 4)
        self.price = Label(self.my_frame3, font=("Impact", "23"), text = 'Price($)').grid(row = 0, column = 8)

        for x in range (len(food)):
            self.foodop = Label(self.my_frame3, font=("Impact", "15"), text = food[x]).grid(row = 3+x, column = 4)
            self.fprice = Label(self.my_frame3, font=("Impact", "15"), text = foodprice[x]).grid(row = 3+x, column = 8)

        for x in range (len(drinks)):
            self.drinksop = Label(self.my_frame3, font=("Impact", "15"), text = drinks[x]).grid(row = 5+(len(food))+x, column = 4)
            self.drinksp = Label(self.my_frame3, font=("Impact", "15"), text = drinksprice[x]).grid(row = 5+(len(food))+x, column = 8)

        self.img_back = PhotoImage(file = 'new-dip-project\\back_button.png')
        self.back_button = Button(self.my_frame3,image = self.img_back, command = self.open_menu, cursor = "hand2", bd  = 0)
        self.back_button.grid(row = 38, column = 7)


    def order_menu(self):

        self.my_frame2.destroy()
        self.my_frame4 = Frame(root)
        self.my_frame4.pack(fill = "both", expand = 1)

        self.tkvar = StringVar(self.my_frame4)
        self.tkvar.set("Food")
        self.tkvar.trace_add('write', lambda *args: print(self.tkvar.get()))

        self.tkvar2 = StringVar(self.my_frame4)
        self.tkvar2.set("Drinks")
        self.tkvar2.trace_add('write', lambda *args: print(self.tkvar2.get()))

        self.img_odmenu = PhotoImage(file = 'new-dip-project\\od_menu.png')
        self.order_menu_message = Label(self.my_frame4, image = self.img_odmenu).place(x = 220)

        self.foodMenu = OptionMenu(self.my_frame4, self.tkvar, *['Pizza','Burger','Nachos', 'French Toast'])
        self.foodMenu.place(x = 160, y = 110)


        self.drinkMenu = OptionMenu(self.my_frame4, self.tkvar2, *['Pepsi','Lemonade','Tea', 'Aperitivo Spritz'])
        self.drinkMenu.place(x = 385, y = 110)


        self.order_btn1 = PhotoImage(file = 'new-dip-project\\orderb.png')
        self.order_button2 = Button(self.my_frame4, text = "SHOW", image = self.order_btn1, command = self.calculate_order, cursor = "hand2", bd = 0)  ## just self.calculate_order
        self.order_button2.place(x = 302, y = 160)

        self.check_btn = PhotoImage(file = 'new-dip-project\\checkpay.png')
        self.checkout_btn = Button(self.my_frame4, image = self.check_btn, cursor = "hand2", bd = 0, command = self.cart_order)  ## just self.cart_order
        self.checkout_btn.place(x = 267, y = 410)

        self.img_back3 = PhotoImage(file = 'new-dip-project\\bbutton.png')
        self.back_button3 = Button(self.my_frame4, image = self.img_back3, cursor = "hand2", bd = 0, command = self.open_menu)
        self.back_button3.place(x = 50, y = 410)

        ## You don't need this. An instance is created in main 
        ##t = final_cart(parent, Goode_brothers)




class final_cart(Goode_brothers):

    ## No need for your own __init__, just inherit the one from Goode_brothers


    def cart_order(self):
        self.root3 = Toplevel(root)
        self.root3.geometry("600x400")

        self.img_back2 = PhotoImage(file = 'new-dip-project\\bbutton.png')
        self.back_button2 = Button(self.root3, image = self.img_back2, cursor = "hand2", bd = 0, command = self.open_menu)
        self.back_button2.grid(column=0, row = 1)

        self.img_cart = PhotoImage(file = 'new-dip-project\\your_cart.png')
        self.y_cart = Label(self.root3, image = self.img_cart).grid(column=3, row = 1)

        Label(self.root3, text = "",font=("Courier New","12")).grid(column = 1, row = 1)

        Label(self.root3, text = "",font=("Courier New","12")).grid(column = 1, row = 4)

        for x in range (len(orderlist)):
            Label(self.root3, text = (str(x+1)),font=("Courier New","12")).grid(column =0, row= x+5)
            Label(self.root3, text = (orderlist[x][0]), font=("Courier New","12")).grid(column=2, row = x+5)




    def calculate_order(self):
       
        print (orderlist)


    def food_optionmenu(self, *args):
        self.position = food.index(self.tkvar.get())

    def drink_optionmenu(self, *args):
        self.position2 = drinks.index(self.tkvar2.get())
        self.tkvar.trace('w', self.food_optionmenu)
        self.tkvar2.trace('w', self.drink_optionmenu)



## main routine
if __name__ == "__main__":
    root = Tk()
    root.geometry('670x466')
    FoodSystem = final_cart(root)   ## Just make an instance of final_cart

    root.title('Goode brothers')
    root.mainloop()

本質上,您已經對Goode_brothers了子類Goode_brothers因此當您實例化final_cart時,這兩個類充當一個。

首先這一行: class final_cart(Goode_brothers):表示final_cart繼承自Goode_brothers

第二行: FoodSystem = final_cart(root)實例化final_cart使兩個類作為一個類。 這意味着任何調用self.anothermethod()都可以訪問來自兩個類的所有方法的聯合。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM