尝试向sqlite提交条目并使用一个按钮更改框架

时间:2018-03-26 18:46:29

标签: python sqlite button tkinter

我遇到了一个问题,我需要能够使用按钮将条目提交到sqlite数据库,还可以使用相同的按钮更改框架。这是一个学校项目,我在编程方面是一个业余爱好者。这是我到目前为止的代码,它返回错误AttributeError: '_tkinter.tkapp' object has no attribute 'confirm_button'

我尝试创建一个名为confirm_button的新函数,但它无效。需要帮助的位在第1页的类中,它是确认按钮。

import tkinter as tk
from tkinter import *
from appJar import gui 
import sqlite3


with sqlite3.connect("Appointments.db") as db:
    c = db.cursor()

c.execute("""CREATE TABLE IF NOT EXISTS Appointments(
    PatientID INTEGER PRIMARY KEY,
    First_Name TEXT NOT NULL,
    Last_Name TEXT NOT NULL,
    DOB DATE NOT NULL,
    Contact_Number INTEGER NOT NULL,
    DOA DATE NOT NULL,
    Start_Time TIME NOT NULL,
    Duration TIME NOT NULL,
    End_Time TIME NOT NULL);""")
db.commit()
db.close()

#-------------------------------------------

class alex(tk.Tk):

    def __init__(self, *args, **kwargs):

        tk.Tk.__init__(self, *args, **kwargs)
        container = tk.Frame(self)

        container.pack(side="top", fill="both", expand = True)

        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)

        self.frames = {}

        for F in (StartPage, PageOne, PageTwo, PageThree, PageFour, PageFive):

            frame = F(container, self)

            self.frames[F] = frame

            frame.grid(row=0, column=0, sticky="nsew")

        self.show_frame(StartPage)

    def show_frame(self, cont):

        frame = self.frames[cont]
        frame.tkraise()

    def insert_user():
            First_Name = fentry.get()
            Last_Name = lentry.get()
            DOB = dobentry.get() 
            Contact_Number = cnumentry.get()
            DOA = doaentry.get()
            Start_Time = sentry.get()
            Duration = dentry.get()
            End_Time = etentry.get()
            cursor.execute(insert_command % (First_Name, Last_Name,
                                         DOB, Contact_Number, DOA, Start_Time, Duration, End_Time))
#--------------------------------------------        

class StartPage(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self,parent)
        label = tk.Label(self, text="Appointment System", bg="#FDFEFE", font=("Helvetica", 70))
        label.pack(pady=10,fill=X)

        book = tk.Button(self, text="Book Appointment",font=("Helvetica", 40),
                            command=lambda: controller.show_frame(PageOne))
        book.config(height=2, width=14)
        book.pack(side=tk.LEFT, padx=4)

        view = tk.Button(self, text="View Appointment",font=("Helvetica", 40),
                            command=lambda: controller.show_frame(PageTwo))
        view.config(height=2, width=14)
        view.pack(side=tk.RIGHT, padx=4)

        find = tk.Button(self, text="Find Appointment",font=("Helvetica", 40),
                            command=lambda: controller.show_frame(PageFour))

        find.config(height=2, width=14)
        find.pack(side=tk.BOTTOM, pady=20)

#-------------------------------------------- 

class PageOne(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        label = tk.Label(self, text="Appointment Booking!",bg="#FDFEFE", font=("Helvetica", 70))
        label.pack(pady=10,fill=X)
        #--------------------
        frame1 = Frame(self)
        frame1.pack(fill=X)

        fname = Label(frame1, text="First Name",font=("Helvetica", 30))
        fname.pack(side=tk.LEFT, padx=50, pady=5)           

        doa = Label(frame1, text="Appointment Date",font=("Helvetica", 30))
        doa.pack(side=tk.RIGHT, padx=175, pady=5)
        #--------------------
        frame2 = Frame(self)
        frame2.pack(fill=X)

        fentry = Entry(frame2,font=("Helvetica", 30))
        fentry.pack(side=tk.LEFT, padx=50)

        doaentry = Entry(frame2,font=("Helvetica", 30))
        doaentry.pack(side=tk.RIGHT, padx=50) 
        #--------------------
        frame3 = Frame(self)
        frame3.pack(fill=BOTH)

        lname = Label(frame3, text="Last Name",font=("Helvetica", 30))
        lname.pack(side=tk.LEFT, padx=50, pady=5)        

        stime = Label(frame3, text="Start Time",font=("Helvetica", 30))
        stime.pack(side=tk.RIGHT, padx=310, pady=5)  
        #--------------------
        frame4 = Frame(self)
        frame4.pack(fill=BOTH)

        lentry = Entry(frame4,font=("Helvetica", 30))
        lentry.pack(side=tk.LEFT, padx=50)

        sentry = Entry(frame4,font=("Helvetica", 30))
        sentry.pack(side=tk.RIGHT, padx=50)
        #--------------------
        frame5 = Frame(self)
        frame5.pack(fill=BOTH)

        dob = Label(frame5, text="D.O.B",font=("Helvetica", 30))
        dob.pack(side=LEFT, padx=50, pady=5)        

        duration = Label(frame5, text="Duration",font=("Helvetica", 30))
        duration.pack(side=tk.RIGHT, padx=345, pady=5)
        #--------------------
        frame6 = Frame(self)
        frame6.pack(fill=BOTH)

        dobentry = Entry(frame6,font=("Helvetica", 30))
        dobentry.pack(side=tk.LEFT, padx=50)

        dentry = Entry(frame6,font=("Helvetica", 30))
        dentry.pack(side=tk.RIGHT, padx=50)
        #--------------------
        frame7 = Frame(self)
        frame7.pack(fill=BOTH)

        cnum = Label(frame7, text="Contact Number",font=("Helvetica", 30))
        cnum.pack(side=LEFT, padx=50, pady=5)        

        etime = Label(frame7, text="End Time",font=("Helvetica", 30))
        etime.pack(side=tk.RIGHT, padx=325, pady=5)
        #--------------------
        frame8 = Frame(self)
        frame8.pack(fill=BOTH)

        cnumentry = Entry(frame8,font=("Helvetica", 30))
        cnumentry.pack(side=tk.LEFT, padx=50)

        etentry = Entry(frame8,font=("Helvetica", 30))
        etentry.pack(side=tk.RIGHT, padx=50)
        #--------------------
        confirm = tk.Button(self, text="Confirm",font=("Helvetica", 30),
                            command=lambda: controller.confirm_button(self))
        confirm.pack(side=tk.LEFT,padx=250)

        cancel = tk.Button(self, text="Cancel",font=("Helvetica", 30),
                            command=lambda: controller.show_frame(StartPage))
        cancel.pack(side=tk.RIGHT,padx=250)

    def insert_user(self):
        First_Name = fentry.get()
        Last_Name = lentry.get()
        DOB = dobentry.get() 
        Contact_Number = cnumentry.get()
        DOA = doaentry.get()
        Start_Time = sentry.get()
        Duration = dentry.get()
        End_Time = etentry.get()
        cursor.execute(insert_command % (First_Name, Last_Name,
                                     DOB, Contact_Number, DOA, Start_Time, Duration, End_Time))

    def confirm_button(self):
        controller.show_frame(PageThree)
        insert_user(self)
#-------------------------------------------- 

class PageTwo(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        label = tk.Label(self, text="Find Appointment!", bg="#FDFEFE", font=("Helvetica", 70))
        label.pack(pady=10,fill=X)

        main = tk.Button(self, text="Return to Main Menu",font=("Helvetica", 30),
                            command=lambda: controller.show_frame(StartPage))
        main.pack()

#-------------------------------------------- 

class PageThree(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        label = tk.Label(self, text="Appointment Booked!", bg="#FDFEFE", font=("Helvetica", 70))
        label.pack(pady=10,fill=X)

        main = tk.Button(self, text="Return to Main Menu",font=("Helvetica", 30),
                            command=lambda: controller.show_frame(StartPage))
        main.pack(pady=200,padx=200)

#-------------------------------------------- 

class PageFour(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        label = tk.Label(self, text="Enter D.O.B!", bg="#FDFEFE", font=("Helvetica", 50))
        label.pack(pady=10,fill=X)

        frame1 = Frame(self)
        frame1.pack(fill=X)

        dobbentry = Entry(frame1, font=("Helvetica", 70), justify="center")
        dobbentry.pack()

        frame2 = Frame(self)
        frame2.pack(fill=X)

        dobb = Label(frame2, text="Please enter in the form DD/MM/YYYY",font=("Helvetica", 30))
        dobb.pack(padx=5, pady=5) 

        confirm = tk.Button(self, text="Confirm",font=("Helvetica", 30),
                            command=lambda: controller.show_frame(PageFive))
        confirm.pack(side=tk.LEFT, padx=200)

        cancel = tk.Button(self, text="Cancel",font=("Helvetica", 30),
                            command=lambda: controller.show_frame(StartPage))
        cancel.pack(side=tk.RIGHT, padx=200)

#-------------------------------------------- 

class PageFive(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        label = tk.Label(self, text="Appointments Found!", bg="#FDFEFE", font=("Helvetica", 70))
        label.pack(pady=10,fill=X)

        main = tk.Button(self, text="Return to Main Menu",
                            command=lambda: controller.show_frame(StartPage))
        main.pack()


app = alex()
app.mainloop()

1 个答案:

答案 0 :(得分:1)

这是(希望)修复后的代码版本。你需要继续开发它(因为我对SQL知之甚少),但应该完成GUI的工作。

注意:只是一点点,我添加了代码以允许DPI识别,因此,在特定设置上,这将使文本更清晰。

import tkinter as tk
from tkinter import *
from appJar import gui 
import sqlite3, ctypes

try: ctypes.windll.shcore.SetProcessDpiAwareness (True)
except: pass

with sqlite3.connect("Appointments.db") as db:
    c = db.cursor()

c.execute("""CREATE TABLE IF NOT EXISTS Appointments(
    PatientID INTEGER PRIMARY KEY,
    First_Name TEXT NOT NULL,
    Last_Name TEXT NOT NULL,
    DOB DATE NOT NULL,
    Contact_Number INTEGER NOT NULL,
    DOA DATE NOT NULL,
    Start_Time TIME NOT NULL,
    Duration TIME NOT NULL,
    End_Time TIME NOT NULL);""")
db.commit()
db.close()

#-------------------------------------------

class alex(tk.Tk):

    def __init__(self, *args, **kwargs):

        tk.Tk.__init__(self, *args, **kwargs)
        container = tk.Frame(self)

        container.pack(side="top", fill="both", expand = True)

        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)

        self.frames = {}

        for F in (StartPage, PageOne, PageTwo, PageThree, PageFour, PageFive):

            frame = F(container, self)

            self.frames[F] = frame

            frame.grid(row=0, column=0, sticky="nsew")

        self.show_frame(StartPage)

    def show_frame(self, cont):

        frame = self.frames[cont]
        frame.tkraise()

    def insert_user():
            First_Name = fentry.get()
            Last_Name = lentry.get()
            DOB = dobentry.get() 
            Contact_Number = cnumentry.get()
            DOA = doaentry.get()
            Start_Time = sentry.get()
            Duration = dentry.get()
            End_Time = etentry.get()
            c.execute(insert_command % (First_Name, Last_Name,
                                         DOB, Contact_Number, DOA, Start_Time, Duration, End_Time))
#--------------------------------------------        

class StartPage(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self,parent)
        label = tk.Label(self, text="Appointment System", bg="#FDFEFE", font=("Helvetica", 70))
        label.pack(pady=10,fill=X)

        book = tk.Button(self, text="Book Appointment",font=("Helvetica", 40),
                            command=lambda: controller.show_frame(PageOne))
        book.config(height=2, width=14)
        book.pack(side=tk.LEFT, padx=4)

        view = tk.Button(self, text="View Appointment",font=("Helvetica", 40),
                            command=lambda: controller.show_frame(PageTwo))
        view.config(height=2, width=14)
        view.pack(side=tk.RIGHT, padx=4)

        find = tk.Button(self, text="Find Appointment",font=("Helvetica", 40),
                            command=lambda: controller.show_frame(PageFour))

        find.config(height=2, width=14)
        find.pack(side=tk.BOTTOM, pady=20)

#-------------------------------------------- 

class PageOne(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        label = tk.Label(self, text="Appointment Booking!",bg="#FDFEFE", font=("Helvetica", 70))
        label.pack(pady=10,fill=X)
        #--------------------
        frame1 = Frame(self)
        frame1.pack(fill=X)

        fname = Label(frame1, text="First Name",font=("Helvetica", 30))
        fname.pack(side=tk.LEFT, padx=50, pady=5)           

        doa = Label(frame1, text="Appointment Date",font=("Helvetica", 30))
        doa.pack(side=tk.RIGHT, padx=175, pady=5)
        #--------------------
        frame2 = Frame(self)
        frame2.pack(fill=X)

        self.fentry = Entry(frame2,font=("Helvetica", 30))
        self.fentry.pack(side=tk.LEFT, padx=50)

        self.doaentry = Entry(frame2,font=("Helvetica", 30))
        self.doaentry.pack(side=tk.RIGHT, padx=50) 
        #--------------------
        frame3 = Frame(self)
        frame3.pack(fill=BOTH)

        lname = Label(frame3, text="Last Name",font=("Helvetica", 30))
        lname.pack(side=tk.LEFT, padx=50, pady=5)        

        stime = Label(frame3, text="Start Time",font=("Helvetica", 30))
        stime.pack(side=tk.RIGHT, padx=310, pady=5)  
        #--------------------
        frame4 = Frame(self)
        frame4.pack(fill=BOTH)

        self.lentry = Entry(frame4,font=("Helvetica", 30))
        self.lentry.pack(side=tk.LEFT, padx=50)

        self.sentry = Entry(frame4,font=("Helvetica", 30))
        self.sentry.pack(side=tk.RIGHT, padx=50)
        #--------------------
        frame5 = Frame(self)
        frame5.pack(fill=BOTH)

        dob = Label(frame5, text="D.O.B",font=("Helvetica", 30))
        dob.pack(side=LEFT, padx=50, pady=5)        

        duration = Label(frame5, text="Duration",font=("Helvetica", 30))
        duration.pack(side=tk.RIGHT, padx=345, pady=5)
        #--------------------
        frame6 = Frame(self)
        frame6.pack(fill=BOTH)

        self.dobentry = Entry(frame6,font=("Helvetica", 30))
        self.dobentry.pack(side=tk.LEFT, padx=50)

        self.dentry = Entry(frame6,font=("Helvetica", 30))
        self.dentry.pack(side=tk.RIGHT, padx=50)
        #--------------------
        frame7 = Frame(self)
        frame7.pack(fill=BOTH)

        cnum = Label(frame7, text="Contact Number",font=("Helvetica", 30))
        cnum.pack(side=LEFT, padx=50, pady=5)        

        etime = Label(frame7, text="End Time",font=("Helvetica", 30))
        etime.pack(side=tk.RIGHT, padx=325, pady=5)
        #--------------------
        frame8 = Frame(self)
        frame8.pack(fill=BOTH)

        self.cnumentry = Entry(frame8,font=("Helvetica", 30))
        self.cnumentry.pack(side=tk.LEFT, padx=50)

        self.etentry = Entry(frame8,font=("Helvetica", 30))
        self.etentry.pack(side=tk.RIGHT, padx=50)
        #--------------------
        confirm = tk.Button(self, text="Confirm",font=("Helvetica", 30),
                            command=lambda: self.confirm_button(controller))
        confirm.pack(side=tk.LEFT,padx=250)

        cancel = tk.Button(self, text="Cancel",font=("Helvetica", 30),
                            command=lambda: controller.show_frame(StartPage))
        cancel.pack(side=tk.RIGHT,padx=250)

    def insert_user(self):
        First_Name = self.fentry.get()
        Last_Name = self.lentry.get()
        DOB = self.dobentry.get() 
        Contact_Number = self.cnumentry.get()
        DOA = self.doaentry.get()
        Start_Time = self.sentry.get()
        Duration = self.dentry.get()
        End_Time = self.etentry.get()
        c.execute(insert_command % (First_Name, Last_Name,
                                     DOB, Contact_Number, DOA, Start_Time, Duration, End_Time))

    def confirm_button(self, controller):
        controller.show_frame(PageThree)
        self.insert_user()
#-------------------------------------------- 

class PageTwo(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        label = tk.Label(self, text="Find Appointment!", bg="#FDFEFE", font=("Helvetica", 70))
        label.pack(pady=10,fill=X)

        main = tk.Button(self, text="Return to Main Menu",font=("Helvetica", 30),
                            command=lambda: controller.show_frame(StartPage))
        main.pack()

#-------------------------------------------- 

class PageThree(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        label = tk.Label(self, text="Appointment Booked!", bg="#FDFEFE", font=("Helvetica", 70))
        label.pack(pady=10,fill=X)

        main = tk.Button(self, text="Return to Main Menu",font=("Helvetica", 30),
                            command=lambda: controller.show_frame(StartMenu))
        main.pack(pady=200,padx=200)

#-------------------------------------------- 

class PageFour(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        label = tk.Label(self, text="Enter D.O.B!", bg="#FDFEFE", font=("Helvetica", 50))
        label.pack(pady=10,fill=X)

        frame1 = Frame(self)
        frame1.pack(fill=X)

        dobbentry = Entry(frame1, font=("Helvetica", 70), justify="center")
        dobbentry.pack()

        frame2 = Frame(self)
        frame2.pack(fill=X)

        dobb = Label(frame2, text="Please enter in the form DD/MM/YYYY",font=("Helvetica", 30))
        dobb.pack(padx=5, pady=5) 

        confirm = tk.Button(self, text="Confirm",font=("Helvetica", 30),
                            command=lambda: controller.show_frame(PageFive))
        confirm.pack(side=tk.LEFT, padx=200)

        cancel = tk.Button(self, text="Cancel",font=("Helvetica", 30),
                            command=lambda: controller.show_frame(StartPage))
        cancel.pack(side=tk.RIGHT, padx=200)

#-------------------------------------------- 

class PageFive(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        label = tk.Label(self, text="Appointments Found!", bg="#FDFEFE", font=("Helvetica", 70))
        label.pack(pady=10,fill=X)

        main = tk.Button(self, text="Return to Main Menu",
                            command=lambda: controller.show_frame(StartPage))
        main.pack()


app = alex()
app.mainloop()