import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk
import sqlite3
class LoginRegisterWindow(Gtk.Window):
def __init__(self):
super().__init__(title="Login and Register")
self.set_default_size(400, 200)
# 创建一个水平盒布局
box = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=10)
# 创建登录面板
self.login_panel = LoginPanel(self)
box.pack_start(self.login_panel, True, True, 0)
# 创建注册面板
self.register_panel = RegisterPanel(self)
box.pack_start(self.register_panel, True, True, 0)
# 将盒布局添加到窗口
self.add(box)
class LoginPanel(Gtk.Box):
def __init__(self, parent):
super().__init__(orientation=Gtk.Orientation.VERTICAL, spacing=10)
# 用户名和密码输入
self.entry_username = Gtk.Entry()
self.entry_password = Gtk.Entry()
self.entry_password.set_visibility(False)
# 登录按钮
self.button_login = Gtk.Button(label="Login")
self.button_login.connect("clicked", self.on_login_clicked)
# 添加到登录面板
self.pack_start(Gtk.Label(label="Username"), False, False, 0)
self.pack_start(self.entry_username, False, False, 0)
self.pack_start(Gtk.Label(label="Password"), False, False, 0)
self.pack_start(self.entry_password, False, False, 0)
self.pack_start(self.button_login, False, False, 0)
self.parent = parent
def on_login_clicked(self, widget):
username = self.entry_username.get_text()
password = self.entry_password.get_text()
if self.validate_login(username, password):
self.parent.set_title("Welcome, %s!" % username)
# 这里可以添加进入主界面的代码
def validate_login(self, username, password):
# 连接数据库并检查凭据
conn = sqlite3.connect('users.db')
cursor = conn.cursor()
cursor.execute("SELECT * FROM users WHERE username=? AND password=?", (username, password))
if cursor.fetchone():
conn.close()
return True
else:
conn.close()
self.entry_username.set_text("")
self.entry_password.set_text("")
return False
class Regis
评论已关闭