使 tkinter 函数不显示在下一个 window
Making a tkinter function not show in next window
我有一个具有登录功能的 tkinter window,允许三个人之一登录机场登录系统,"HQ"、"Pilot/Crew" 或 "customer"。
到目前为止,我可以创建用户并且可以使用正在使用的 sql 登录。
问题是我不确定如何将 Dropbox 变成小部件,因此它不会在接下来的 window 中消失。
# imports
from tkinter import *
import tkinter as tk
from tkinter import messagebox as 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 (var TEXT NOT NULL, username TEXT NOT NULL ,password TEXT NOT NULL);')
db.commit()
db.close()
# main Class
class main:
def __init__(self, master):
# Window
self.master = master
# Some Usefull variables
self.var = tk.StringVar()
self.username = StringVar()
self.password = StringVar()
self.n_username = StringVar()
self.n_password = StringVar()
# Create Widgets
self.widgets()
# def pack():
# if self.var.get() = "HQ"
# 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 var = ? and username = ? and password = ?')
c.execute(find_user, [(self.var.get()), (self.username.get()), (self.password.get())])
result = c.fetchall()
if result:
self.logf.pack_forget()
self.head['text'] = self.username.get() + '\n Loged In'
self.head['pady'] = 150
else:
ms.showerror('Oops!', 'something is not right.')
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(var,username,password) VALUES(?,?,?)'
c.execute(insert, [(self.var.get()), (self.n_username.get()), (self.n_password.get())])
db.commit()
def dropbox(self):
OPTIONS = [
"Please Select",
"HQ",
"Pilot/Crew",
"Customer"
]
self.var.set(OPTIONS[0])
dropdownmenu = OptionMenu(root, self.var, OPTIONS[0], OPTIONS[1], OPTIONS[2], OPTIONS[3])
dropdownmenu.pack()
# 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=('', 35), pady=10)
self.head.pack()
self.logf = Frame(self.master, padx=10, pady=10)
self.dropbox()
Label(self.logf, text='Username: ', font=('', 20), pady=5, padx=5).grid(row=2, column=0)
Entry(self.logf, textvariable=self.username, bd=5, font=('', 15)).grid(row=2, column=1)
Label(self.logf, text='Password: ', font=('', 20), pady=5, padx=5).grid(sticky=W)
Entry(self.logf, textvariable=self.password, bd=5, font=('', 15), show='*').grid(row=3, column=1)
Button(self.logf, text='Login', bd=3, font=('', 15), padx=5, pady=5, command=self.login).grid()
Button(self.logf, text='Create Account', bd=3, font=('', 15), padx=5, pady=5, command=self.cr).grid(row=4, column=1)
self.logf.pack()
self.crf = Frame(self.master, padx=10, pady=10)
Label(self.crf, text='Select:', font=('', 20), pady=5, padx=5).grid(row=1, column=0)
Label(self.crf, text='Username: ', font=('', 20), pady=5, padx=5).grid(sticky=W)
Entry(self.crf, textvariable=self.n_username, bd=5, font=('', 15)).grid(row=2, column=1)
Label(self.crf, text='Password: ', font=('', 20), pady=5, padx=5).grid(sticky=W)
Entry(self.crf, textvariable=self.n_password, bd=5, font=('', 15), show='*').grid(row=3, column=1)
Button(self.crf, text='Create Account', bd=3, font=('', 15), padx=5, pady=5, command=self.new_user).grid()
Button(self.crf, text='Go to Login', bd=3, font=('', 15), padx=5, pady=5, command=self.log).grid(row=4, column=1)
# create window and application object
root = tk.Tk()
# root.title("Login Form")
main(root)
root.mainloop()
这是我目前的代码,它有效。
还有人知道如何将保管箱从 .pack() 更改为 .grid(),因为我想找到它,我放弃了,但它仍然困扰着我
您可以修改 dropbox()
使其多一个参数:父级和 return 创建的 OptionMenu
(使用 parent
参数作为其父级而不是 root
) 如下所示:
def dropbox(self, parent):
OPTIONS = [
"Please Select",
"HQ",
"Pilot/Crew",
"Customer"
]
self.var.set(OPTIONS[0])
return OptionMenu(parent, self.var, *OPTIONS)
我已经删除了 .pack()
的执行,以便您以后可以使用任何您想要的布局管理器。
然后修改widgets()
如下:
def widgets(self):
self.head = Label(self.master, text='LOGIN', font=('', 35), pady=10)
self.head.pack()
self.logf = Frame(self.master, padx=10, pady=10)
self.dropbox(self.logf).grid(row=1, column=0, columnspan=2, pady=10)
Label(self.logf, text='Username: ', font=('', 20), pady=5, padx=5).grid(row=2, column=0)
Entry(self.logf, textvariable=self.username, bd=5, font=('', 15)).grid(row=2, column=1)
Label(self.logf, text='Password: ', font=('', 20), pady=5, padx=5).grid(sticky=W)
Entry(self.logf, textvariable=self.password, bd=5, font=('', 15), show='*').grid(row=3, column=1)
Button(self.logf, text='Login', bd=3, font=('', 15), padx=5, pady=5, command=self.login).grid()
Button(self.logf, text='Create Account', bd=3, font=('', 15), padx=5, pady=5, command=self.cr).grid(row=4, column=1)
self.logf.pack()
self.crf = Frame(self.master, padx=10, pady=10)
Label(self.crf, text='Select:', font=('', 20), pady=5, padx=5).grid(row=1, column=0)
self.dropbox(self.crf).grid(row=1, column=1, sticky=W)
Label(self.crf, text='Username: ', font=('', 20), pady=5, padx=5).grid(sticky=W)
Entry(self.crf, textvariable=self.n_username, bd=5, font=('', 15)).grid(row=2, column=1)
Label(self.crf, text='Password: ', font=('', 20), pady=5, padx=5).grid(sticky=W)
Entry(self.crf, textvariable=self.n_password, bd=5, font=('', 15), show='*').grid(row=3, column=1)
Button(self.crf, text='Create Account', bd=3, font=('', 15), padx=5, pady=5, command=self.new_user).grid()
Button(self.crf, text='Go to Login', bd=3, font=('', 15), padx=5, pady=5, command=self.log).grid(row=4, column=1)
我还认为您代码中以下块的缩进是错误的:
with sqlite3.connect('quit.db') as db:
c = db.cursor()
# wrong indentation?
c.execute('CREATE TABLE IF NOT EXISTS user (var TEXT NOT NULL, username TEXT NOT NULL ,password TEXT NOT NULL);')
db.commit()
db.close()
应该是:
with sqlite3.connect('quit.db') as db:
c = db.cursor()
c.execute('CREATE TABLE IF NOT EXISTS user (var TEXT NOT NULL, username TEXT NOT NULL ,password TEXT NOT NULL);')
db.commit()
# no need to call db.close() as the db will be closed when exiting the with block
类似的问题适用于您代码后面的其他 with sqlite3.connect(...) as db
语句。
我有一个具有登录功能的 tkinter window,允许三个人之一登录机场登录系统,"HQ"、"Pilot/Crew" 或 "customer"。
到目前为止,我可以创建用户并且可以使用正在使用的 sql 登录。
问题是我不确定如何将 Dropbox 变成小部件,因此它不会在接下来的 window 中消失。
# imports
from tkinter import *
import tkinter as tk
from tkinter import messagebox as 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 (var TEXT NOT NULL, username TEXT NOT NULL ,password TEXT NOT NULL);')
db.commit()
db.close()
# main Class
class main:
def __init__(self, master):
# Window
self.master = master
# Some Usefull variables
self.var = tk.StringVar()
self.username = StringVar()
self.password = StringVar()
self.n_username = StringVar()
self.n_password = StringVar()
# Create Widgets
self.widgets()
# def pack():
# if self.var.get() = "HQ"
# 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 var = ? and username = ? and password = ?')
c.execute(find_user, [(self.var.get()), (self.username.get()), (self.password.get())])
result = c.fetchall()
if result:
self.logf.pack_forget()
self.head['text'] = self.username.get() + '\n Loged In'
self.head['pady'] = 150
else:
ms.showerror('Oops!', 'something is not right.')
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(var,username,password) VALUES(?,?,?)'
c.execute(insert, [(self.var.get()), (self.n_username.get()), (self.n_password.get())])
db.commit()
def dropbox(self):
OPTIONS = [
"Please Select",
"HQ",
"Pilot/Crew",
"Customer"
]
self.var.set(OPTIONS[0])
dropdownmenu = OptionMenu(root, self.var, OPTIONS[0], OPTIONS[1], OPTIONS[2], OPTIONS[3])
dropdownmenu.pack()
# 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=('', 35), pady=10)
self.head.pack()
self.logf = Frame(self.master, padx=10, pady=10)
self.dropbox()
Label(self.logf, text='Username: ', font=('', 20), pady=5, padx=5).grid(row=2, column=0)
Entry(self.logf, textvariable=self.username, bd=5, font=('', 15)).grid(row=2, column=1)
Label(self.logf, text='Password: ', font=('', 20), pady=5, padx=5).grid(sticky=W)
Entry(self.logf, textvariable=self.password, bd=5, font=('', 15), show='*').grid(row=3, column=1)
Button(self.logf, text='Login', bd=3, font=('', 15), padx=5, pady=5, command=self.login).grid()
Button(self.logf, text='Create Account', bd=3, font=('', 15), padx=5, pady=5, command=self.cr).grid(row=4, column=1)
self.logf.pack()
self.crf = Frame(self.master, padx=10, pady=10)
Label(self.crf, text='Select:', font=('', 20), pady=5, padx=5).grid(row=1, column=0)
Label(self.crf, text='Username: ', font=('', 20), pady=5, padx=5).grid(sticky=W)
Entry(self.crf, textvariable=self.n_username, bd=5, font=('', 15)).grid(row=2, column=1)
Label(self.crf, text='Password: ', font=('', 20), pady=5, padx=5).grid(sticky=W)
Entry(self.crf, textvariable=self.n_password, bd=5, font=('', 15), show='*').grid(row=3, column=1)
Button(self.crf, text='Create Account', bd=3, font=('', 15), padx=5, pady=5, command=self.new_user).grid()
Button(self.crf, text='Go to Login', bd=3, font=('', 15), padx=5, pady=5, command=self.log).grid(row=4, column=1)
# create window and application object
root = tk.Tk()
# root.title("Login Form")
main(root)
root.mainloop()
这是我目前的代码,它有效。
还有人知道如何将保管箱从 .pack() 更改为 .grid(),因为我想找到它,我放弃了,但它仍然困扰着我
您可以修改 dropbox()
使其多一个参数:父级和 return 创建的 OptionMenu
(使用 parent
参数作为其父级而不是 root
) 如下所示:
def dropbox(self, parent):
OPTIONS = [
"Please Select",
"HQ",
"Pilot/Crew",
"Customer"
]
self.var.set(OPTIONS[0])
return OptionMenu(parent, self.var, *OPTIONS)
我已经删除了 .pack()
的执行,以便您以后可以使用任何您想要的布局管理器。
然后修改widgets()
如下:
def widgets(self):
self.head = Label(self.master, text='LOGIN', font=('', 35), pady=10)
self.head.pack()
self.logf = Frame(self.master, padx=10, pady=10)
self.dropbox(self.logf).grid(row=1, column=0, columnspan=2, pady=10)
Label(self.logf, text='Username: ', font=('', 20), pady=5, padx=5).grid(row=2, column=0)
Entry(self.logf, textvariable=self.username, bd=5, font=('', 15)).grid(row=2, column=1)
Label(self.logf, text='Password: ', font=('', 20), pady=5, padx=5).grid(sticky=W)
Entry(self.logf, textvariable=self.password, bd=5, font=('', 15), show='*').grid(row=3, column=1)
Button(self.logf, text='Login', bd=3, font=('', 15), padx=5, pady=5, command=self.login).grid()
Button(self.logf, text='Create Account', bd=3, font=('', 15), padx=5, pady=5, command=self.cr).grid(row=4, column=1)
self.logf.pack()
self.crf = Frame(self.master, padx=10, pady=10)
Label(self.crf, text='Select:', font=('', 20), pady=5, padx=5).grid(row=1, column=0)
self.dropbox(self.crf).grid(row=1, column=1, sticky=W)
Label(self.crf, text='Username: ', font=('', 20), pady=5, padx=5).grid(sticky=W)
Entry(self.crf, textvariable=self.n_username, bd=5, font=('', 15)).grid(row=2, column=1)
Label(self.crf, text='Password: ', font=('', 20), pady=5, padx=5).grid(sticky=W)
Entry(self.crf, textvariable=self.n_password, bd=5, font=('', 15), show='*').grid(row=3, column=1)
Button(self.crf, text='Create Account', bd=3, font=('', 15), padx=5, pady=5, command=self.new_user).grid()
Button(self.crf, text='Go to Login', bd=3, font=('', 15), padx=5, pady=5, command=self.log).grid(row=4, column=1)
我还认为您代码中以下块的缩进是错误的:
with sqlite3.connect('quit.db') as db:
c = db.cursor()
# wrong indentation?
c.execute('CREATE TABLE IF NOT EXISTS user (var TEXT NOT NULL, username TEXT NOT NULL ,password TEXT NOT NULL);')
db.commit()
db.close()
应该是:
with sqlite3.connect('quit.db') as db:
c = db.cursor()
c.execute('CREATE TABLE IF NOT EXISTS user (var TEXT NOT NULL, username TEXT NOT NULL ,password TEXT NOT NULL);')
db.commit()
# no need to call db.close() as the db will be closed when exiting the with block
类似的问题适用于您代码后面的其他 with sqlite3.connect(...) as db
语句。