Search code examples
pythontkinter

tkinter has no attribute ticket_price


I have code that works perfectly like this

class TicketSystem(tk.Tk):
    def __init__(self):
        super().__init__()
        self.title("Ticket System")
        self.geometry("1200x800")

        # Use the current working directory to store the database file
        self.database_filename = os.path.join(os.getcwd(), r"C:\Users\M S I\Downloads\Wisatasolo\database_tiket.xlsx")
        if not os.path.exists(self.database_filename):
            df = pd.DataFrame(columns=["Name", "Date", "People", "Total Cost", "Resi Code", "Payment Method"])
            df.to_excel(self.database_filename, index=False)

        self.create_main_widgets()
        
    def create_main_widgets(self):
        self.clear_window()
        self.label = tk.Label(self, text="Pilih Opsi:")
        self.label.pack(pady=10)

        self.reschedule_button = tk.Button(self, text="Reschedule", command=self.reschedule)
        self.reschedule_button.pack(pady=5)

        self.refund_button = tk.Button(self, text="Refund", command=self.refund)
        self.refund_button.pack(pady=5)

        self.history_button = tk.Button(self, text="Riwayat Pembelian", command=self.view_history)
        self.history_button.pack(pady=5)

        self.book_ticket_button = tk.Button(self, text="Book Ticket", command=self.book_ticket)
        self.book_ticket_button.pack(pady=5)

    def return_funds(self):
        # Logika untuk mengembalikan dana 80% dari pembayaran
        messagebox.showinfo("Success", "Dana kembali sebesar 80% dari pembayaran. Refund berhasil")
        self.create_main_widgets()

    def view_history(self):
        self.clear_window()
        # Logika untuk menampilkan riwayat pembelian tiket
        history = "Menampilkan riwayat pembelian..."
        self.label = tk.Label(self, text=history)
        self.label.pack(pady=10)
        self.back_button = tk.Button(self, text="Back", command=self.create_main_widgets)
        self.back_button.pack(pady=5)

    def book_ticket(self):
        self.clear_window()

        self.attractions = [
            "Solo Safari",
            "Lokananta",
            "Tumurun",
            "Keraton Mangkunegaran",
            "Balekambang"
        ]

        self.selected_attraction = tk.StringVar()
        self.attraction_label = tk.Label(self, text="Select Attraction:")
        self.attraction_label.pack(pady=10)
        self.attraction_combo = ttk.Combobox(self, textvariable=self.selected_attraction, values=self.attractions)
        self.attraction_combo.pack(pady=10)
        self.attraction_combo.bind("<<ComboboxSelected>>", self.display_info)

        self.info_label = tk.Label(self, text=" ", wraplength=500)
        self.info_label.pack(pady=10)

        self.data_label = tk.Label(self, text="Enter your details:")
        self.data_label.pack(pady=10)

        self.name_label = tk.Label(self, text="Name:")
        self.name_label.pack(pady=5)
        self.name_entry = tk.Entry(self)
        self.name_entry.pack(pady=5)

        self.date_label = tk.Label(self, text="Date (dd-mm-yyyy):")
        self.date_label.pack(pady=5)
        self.date_entry = tk.Entry(self)
        self.date_entry.pack(pady=5)

        self.people_label = tk.Label(self, text="Number of People:")
        self.people_label.pack(pady=5)
        self.people_entry = tk.Entry(self)
        self.people_entry.pack(pady=5)

        self.payment_label = tk.Label(self, text="Payment Method:")
        self.payment_label.pack(pady=10)

        self.payment_methods = ["Credit Card", "Debit Card", "PayPal"]
        self.selected_payment = tk.StringVar()
        self.payment_combo = ttk.Combobox(self, textvariable=self.selected_payment, values=self.payment_methods)
        self.payment_combo.pack(pady=10)

        self.calculate_button = tk.Button(self, text="Calculate Total", command=self.calculate_total)
        self.calculate_button.pack(pady=20)

        self.total_label = tk.Label(self, text=" ")
        self.total_label.pack(pady=10)

        self.back_button = tk.Button(self, text="Back", command=self.create_main_widgets)
        self.back_button.pack(pady=5)

        self.print_button = tk.Button(self, text="Print Ticket", command=self.print_ticket)
        self.print_button.pack(pady=10)

        self.back_button = tk.Button(self, text="Back", command=self.create_main_widgets)
        self.back_button.pack(pady=5)

    def display_info(self, event):
        attraction = self.selected_attraction.get()
        info_text = " "
        if attraction == "Solo Safari":
            info_text = "Solo Safari: Rp50,000 per person"
            self.ticket_price = 50000
        elif attraction == "Lokananta":
            info_text = "Lokananta: Rp30,000 per person"
            self.ticket_price = 30000
        elif attraction == "Tumurun":
            info_text = "Tumurun: Rp25,000 per person"
            self.ticket_price = 25000
        elif attraction == "Keraton Mangkunegaran":
            info_text = "Keraton Mangkunegaran: Rp40,000 per person"
            self.ticket_price = 40000
        elif attraction == "Balekambang":
            info_text = "Balekambang: Rp35,000 per person"
            self.ticket_price = 35000

        self.info_label.config(text=info_text)

    def calculate_total(self):
        try:
            self.name = self.name_entry.get().strip()
            self.date = self.date_entry.get().strip()
            self.people = int(self.people_entry.get())
            self.payment_method = self.selected_payment.get()

          

            self.total_cost = self.people * self.ticket_price
            self.resi_code = self.generate_resi()

            # Simpan resi ke dalam file Excel
            self.save_resi_to_excel(self.name, self.date, self.people, self.total_cost, self.resi_code, self.payment_method)

            self.total_label.config(text=f"Total Cost: Rp{self.total_cost}\nResi Code: {self.resi_code}")
            messagebox.showinfo("Booking Successful", f"Name: {self.name}\nDate: {self.date}\nPeople: {self.people}\nPayment Method: {self.payment_method}\nTotal Cost: Rp{self.total_cost}\nResi Code: {self.resi_code}")
        except ValueError as ve:
            messagebox.showerror("Input Error", str(ve))
        except Exception as e:
            messagebox.showerror("Error", str(e))

    def generate_resi(self):
        resi_code = ''.join(random.choices(string.ascii_uppercase + string.digits, k=8))
        return resi_code

but if I try to connect it with another python file called main program like this

def show_main(username=None):
    root.withdraw()
    global window
    window = tk.Toplevel(root)
    window.title("Selamat Datang")
    window.geometry("1200x800")
    window.configure(bg="White")
    window.resizable(True, True)

    new_bg_image = Image.open(r"C:\Users\M S I\Downloads\Wisatasolo\Sapa.png")
    new_bg_image = new_bg_image.resize((1200, 800), Image.Resampling.LANCZOS)
    new_photo = ImageTk.PhotoImage(new_bg_image)`your text`

    background_label = tk.Label(window, image=new_photo)
    background_label.place(x=0, y=0, relwidth=1, relheight=1)
    background_label.image = new_photo
    
    new_frame = Frame(window, width=350, height=100, bg="#354f00")
    new_frame.place(x=600, y=600)

    Button(new_frame, width=38, height=1, text="Lanjutkan",command=back_login2, fg="white", bg="Black").place(x=40, y=20)
    Button(new_frame, width=38, height=1, text="Logout", fg="white", bg="#FF0000", command=back_login).place(x=40, y=50)

    def new_background(event=None):
        width = window.winfo_width()
        height = window.winfo_height()
        resized_image = new_bg_image.resize((width, height), Image.Resampling.LANCZOS)
        new_photo = ImageTk.PhotoImage(resized_image)
        background_label.config(image=new_photo)
        background_label.image = new_photo

    window.bind("<Configure>", new_background)
    window.mainloop()
    

def back_login():
    window.destroy()
    root.deiconify()

def back_login2():
    window.destroy()
    instance_kelas = TicketSystem()

An error appears, namely that Tkinter has no attribute ticket_price the problem is when i try to run the first code it will run fine, but when i try to connect the two codes it gives an error

Please help me to solve the error


Solution

  • You've correctly passed the tk.Tk object to the class TicketSystem. Therefore, the methods of tk.Tk can be called. However, you forgot to pass your own variable in the __init__ method, which is not a Tkinter function or attribute.

    Therefore, you get this error:

    Tkinter has no attribute ticket_price   
    
    

    By default, when you say self, Python looks for self within the class you already have. If it does not find the attribute, it would go up to the class you've inherited from (if any), and if not found there, it will return an AttributeError.