Как просмотреть целочисленный столбец в tkinter python с sqlite3 - PullRequest
0 голосов
/ 31 октября 2019

Итак, я создаю портал входа в систему с помощью Tkinter и храню данные в файле .db. Я хочу, чтобы пользователь мог войти в систему и нажать кнопку, которая отображает его банковский баланс. В данный момент у меня все это работает, но когда я вхожу в систему и нажимаю кнопку, чтобы просмотреть баланс, он показывает целое число, но перед ним стоит {none none none}? Если вы хотите исправить код, вы можете это сделать;) (см. Прикрепленный снимок экрана)

GUI output with sql error # импорт из tkinter import * из окна сообщений импорта tkinter как ms import sqlite3

# make database and users (if not exists already) table at programme start up
with sqlite3.connect('quit.db') as db:
    c = db.cursor()

c.execute('CREATE TABLE IF NOT EXISTS user (username TEXT NOT NULL ,password TEX NOT NULL, balance INTEGER NOT NULL);')

db.commit()
# db.close()

#main Class
class main:

    def __init__(self,master):
        # Window 
        self.master = master
        # Some Usefull variables
        self.username = StringVar()
        self.password = StringVar()
        self.n_username = StringVar()
        self.n_password = StringVar()
        self.bal = IntVar()
        self.total = IntVar()
        #Create Widgets
        self.widgets()


        #Login Function
    def login(self):
        #Establish Connection
        with sqlite3.connect('quit.db') as db:
            c = db.cursor()

        #Find user If there is any take proper action
        find_user = ('SELECT * FROM user WHERE username = ? and password = ?')
        c.execute(find_user,[(self.username.get()),(self.password.get())])
        result = c.fetchall()
        if result:
            self.logf.pack_forget()
            # add here the new page 

            self.head['text'] = self.username.get() + '\n Logged In'
            self.head['pady'] = 50
            # Button(self.head,text = 'Activity',bd = 1 ,font = ('Calibri',10),width=8, height=1, command=quit).place(x=0, y=0)

            actionBtn = Button(root, text="Balance", width=8, height=1, command=self.balance).place(x=20, y=0)
            actionBtn2 = Button(root, text="Request", width=8, height=1, command=quit).place(x=95, y=0)

        else:
            ms.showerror('Oops!','Username Not Found.')

    def new_user(self):
        #Establish Connection
        with sqlite3.connect('quit.db') as db:
            c = db.cursor()

        #Find Existing username if any take proper action
        find_user = ('SELECT * FROM user WHERE username = ?')
        c.execute(find_user,[(self.username.get())])        
        if c.fetchall():
            ms.showerror('Error!','Username Taken Try a Diffrent One.')
        else:
            ms.showinfo('Success!','Account Created!')
            self.log()
        #Create New Account 
        insert = 'INSERT INTO user(username,password) VALUES(?,?)'
        c.execute(insert,[(self.n_username.get()),(self.n_password.get())])
        db.commit()

        #Frame Packing Methords
    def log(self):
        self.username.set('')
        self.password.set('')
        self.crf.pack_forget()
        self.head['text'] = 'Login'

        # self.logf.pack()

    def cr(self):
        self.n_username.set('')
        self.n_password.set('')
        self.logf.pack_forget()
        self.head['text'] = 'Create Account'
        self.crf.pack()

    #Draw Widgets
    def widgets(self):
        self.head = Label(self.master,text = 'Login',font = ('Calibri',25),pady = 10)
        self.head.pack()
        self.logf = Frame(self.master,padx =10,pady = 10)
        Label(self.logf,text = 'Username: ',font = ('Calibri',20),pady=5,padx=5).grid(sticky = W)
        Entry(self.logf,textvariable = self.username,bd = 5,font = ('',15)).grid(row=0,column=1)
        Label(self.logf,text = 'Password: ',font = ('Calibri',20),pady=5,padx=5).grid(sticky = W)
        Entry(self.logf,textvariable = self.password,bd = 5,font = ('',15),show = '*').grid(row=1,column=1)
        Button(self.logf,text = ' Login ',bd = 1 ,font = ('Calibri',10),padx=5,pady=5,command=self.login).grid()
        Button(self.logf,text = ' Create Account ',bd = 1 ,font = ('Calibri',10),padx=5,pady=5,command=self.cr).grid(row=2,column=1)
        self.logf.pack()

        self.crf = Frame(self.master,padx =10,pady = 10)
        Label(self.crf,text = 'Username: ',font = ('Calibri',20),pady=5,padx=5).grid(sticky = W)
        Entry(self.crf,textvariable = self.n_username,bd = 5,font = ('',15)).grid(row=0,column=1)
        Label(self.crf,text = 'Password: ',font = ('Calibri',20),pady=5,padx=5).grid(sticky = W)
        Entry(self.crf,textvariable = self.n_password,bd = 5,font = ('',15),show = '*').grid(row=1,column=1)
        Button(self.crf,text = 'Create Account',bd = 1 ,font = ('Calibri',10),padx=5,pady=5,command=self.new_user).grid()

        Button(self.crf,text = 'Go to Login',bd = 1 ,font = ('Calibri',10),padx=5,pady=5,command=self.log).grid(row=2,column=1)

    '''

    def activity(self):
        self.head = Label(self.master,text = 'Login',font = ('Calibri',25),pady = 10)
        self.head.pack()
        self.logf = Frame(self.master,padx =10,pady = 10)
        Label(self.logf,text = 'Username: ',font = ('Calibri',20),pady=5,padx=5).grid(sticky = W)
        Entry(self.logf,textvariable = self.username,bd = 5,font = ('',15)).grid(row=0,column=1)
        Label(self.logf,text = 'Password: ',font = ('Calibri',20),pady=5,padx=5).grid(sticky = W)
        Entry(self.logf,textvariable = self.password,bd = 5,font = ('',15),show = '*').grid(row=1,column=1)
        Button(self.logf,text = ' Login ',bd = 1 ,font = ('Calibri',10),padx=5,pady=5,command=self.deposit).grid()
        Button(self.logf,text = ' Create Account ',bd = 1 ,font = ('Calibri',10),padx=5,pady=5,command=self.withdraw).grid(row=2,column=1)
        self.logf.pack()

    '''


    def balance(self):

        total = ('SELECT balance FROM user')
        total2 = c.execute(total) 
        result2 = c.fetchall()      

        # self.head = Label(self.master,text = 'Balance',font = ('Calibri',25),pady = 10)
        # self.bal.pack()
        self.bal = Label(self.master,text = ('Balance', result2) ,font = ('Calibri',25),pady = 10)
        self.bal.pack()
        self.bal = Frame(self.master,padx =10,pady = 10)




root = Tk()
#root.title("Login Form")
main(root)
root.mainloop()
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...