def register_window(): layout = [ [sg.Text('注册')], [sg.Text('用户名'), sg.InputText(key='name')], [sg.Text('密码'), sg.InputText(key='password', password_char='*')], [sg.Button('注册'), sg.Button('取消')] ] window = sg.Window('ATM').Layout(layout) while True: event, values = window.Read() if event in (None, '取消'): break elif event == '注册': name = values['name'] password = values['password'] for user in users: if user.name == name: sg.Popup('用户名已存在') break else: users.append(User(name, password)) sg.Popup('注册成功') break window.Close()
时间: 2024-02-14 09:35:35 浏览: 12
这是一个用 PySimpleGUI 编写的注册窗口,其中包含用户名和密码的输入框,以及注册和取消按钮。当用户点击注册按钮时,程序将会检查用户名是否已经存在,如果存在,则会弹出一个警告窗口;如果用户名不存在,则会将新用户添加到用户列表中,并弹出一个成功注册的窗口。如果用户点击取消按钮,则程序会退出注册窗口。
相关问题
def user_login(): # 创建用户登录窗口 user_login_window = tk.Toplevel() #Toplevel 是一个容器控件,用来提供一个单独的对话框 user_login_window.title("用户登录界面") # 创建标签和按钮 label = tk.Label(user_login_window, text="请输入用户名和密码:") label.pack() # 将Label组件添加到窗口中显示 username_label = tk.Label(user_login_window, text="用户名:") username_label.pack() username_entry = tk.Entry(user_login_window) username_entry.pack() # 显示输入框 password_label = tk.Label(user_login_window, text="密码:") password_label.pack() # 将Label组件添加到窗口中显示 password_entry = tk.Entry(user_login_window, show="*") password_entry.pack() # 显示输入框 login_button = tk.Button(user_login_window, text="登录", command=lambda: user_login(username_entry.get(), password_entry.get())) login_button.pack() # 进入消息循环 user_login_window.mainloop()
在你的代码中,`user_login()` 函数是一个无参函数,但是在按钮的命令中,你尝试传递了 `username_entry.get()` 和 `password_entry.get()` 这两个参数。这导致了错误的发生,因为 `user_login()` 函数无法接收这两个参数。
为了解决这个问题,你可以将按钮的命令设置为一个新的函数,然后在这个新函数中调用 `user_login()` 函数,并传递参数。
以下是修改后的代码示例:
```python
def user_login():
# 创建用户登录窗口
user_login_window = tk.Toplevel()
user_login_window.title("用户登录界面")
# 创建标签和按钮
label = tk.Label(user_login_window, text="请输入用户名和密码:")
label.pack()
username_label = tk.Label(user_login_window, text="用户名:")
username_label.pack()
username_entry = tk.Entry(user_login_window)
username_entry.pack()
password_label = tk.Label(user_login_window, text="密码:")
password_label.pack()
password_entry = tk.Entry(user_login_window, show="*")
password_entry.pack()
login_button = tk.Button(user_login_window, text="登录", command=login)
login_button.pack()
# 进入消息循环
user_login_window.mainloop()
def login():
# 获取用户名和密码
username = username_entry.get()
password = password_entry.get()
# 在这里进行用户登录的逻辑处理
# 调用用户登录函数
user_login()
```
通过将按钮的命令设置为 `login` 函数,我们可以在该函数中获取用户名和密码,并在需要的地方进行用户登录的逻辑处理。请根据你的需求修改 `login` 函数中的代码。
import requests from bs4 import BeautifulSoup import openpyxl class LianJiaSpider(): def __init__(self): self.url = 'https://bj.lianjia.com/ershoufang/pg{0}/' self.headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36 SLBrowser/8.0.0.12022 SLBChan/109'} def send_request(self, url): resp = requests.get(url, headers=self.headers) if resp.status_code == 200: return resp def parse_html(self, resp): lst = [] html = resp.text bs = BeautifulSoup(html, 'lxml') ul = bs.find('ul', class_='sellListContent') li_list = ul.find_all('li') for item in li_list: title = item.find('div', class_='title').text positionInfo = item.find('div', class_='positionInfo').text address = item.find('div', class_='address').text followInfo = item.find('div', class_='followInfo').text tag = item.find('div', class_='tag').text totalPrice = item.find('div', class_='totalPrice totalPrice2').text unitPrice = item.find('div', class_='unitPrice').text # print(unitPrice) lst.append((title, positionInfo, address, followInfo, tag, totalPrice, unitPrice)) print(lst) self.save(lst) def save(self, lst): wb = openpyxl.Workbook() sheet = wb.active for row in lst: sheet.append(row) continue wb.save('D:/爬虫/链家.csv') def start(self): for i in range(1, 5): full_url = self.url.format(i) resp = self.send_request(full_url) #print(resp.text) self.parse_html(resp) if __name__ == '__main__': lianjia = LianJiaSpider() lianjia.start()使用以上代码爬取数据保存到文件中只显示最后一页30条数据,前面页码的数据都被覆盖了,如何更改
在 `save` 方法中,每次循环都重新创建了一个新的 `Workbook` 对象,因此在保存数据时会覆盖之前的数据。您可以将 `Workbook` 对象的创建放在 `__init__` 方法中,这样每次保存数据时都可以使用同一个 `Workbook` 对象。另外,您还可以将文件名中的页码部分替换为变量 `i`,这样每次保存的文件都会有不同的文件名,不会被覆盖。修改后的代码如下:
```python
import requests
from bs4 import BeautifulSoup
import openpyxl
class LianJiaSpider():
def __init__(self):
self.url = 'https://bj.lianjia.com/ershoufang/pg{0}/'
self.headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36 SLBrowser/8.0.0.12022 SLBChan/109'}
self.wb = openpyxl.Workbook()
self.sheet = self.wb.active
self.sheet.append(('标题', '位置', '地址', '关注信息', '标签', '总价', '单价'))
def send_request(self, url):
resp = requests.get(url, headers=self.headers)
if resp.status_code == 200:
return resp
def parse_html(self, resp):
html = resp.text
bs = BeautifulSoup(html, 'lxml')
ul = bs.find('ul', class_='sellListContent')
li_list = ul.find_all('li')
for item in li_list:
title = item.find('div', class_='title').text
positionInfo = item.find('div', class_='positionInfo').text
address = item.find('div', class_='address').text
followInfo = item.find('div', class_='followInfo').text
tag = item.find('div', class_='tag').text
totalPrice = item.find('div', class_='totalPrice totalPrice2').text
unitPrice = item.find('div', class_='unitPrice').text
self.sheet.append((title, positionInfo, address, followInfo, tag, totalPrice, unitPrice))
print('第{}页数据爬取完成'.format(i))
def save(self):
self.wb.save('D:/爬虫/链家_{}.csv'.format(i))
def start(self):
for i in range(1, 5):
full_url = self.url.format(i)
resp = self.send_request(full_url)
self.parse_html(resp)
self.save()
if __name__ == '__main__':
lianjia = LianJiaSpider()
lianjia.start()
```
在修改后的代码中,我将文件名改为了 `"D:/爬虫/链家_{}.csv".format(i)`,这样每次保存的文件名都会包含页码,不会被覆盖。同时,我将保存数据的操作从 `parse_html` 方法中分离出来,放在了单独的 `save` 方法中,并且在 `__init__` 方法中创建了 `Workbook` 对象和表头。这样就可以避免每次循环都重新创建一个新的 `Workbook` 对象,从而避免了数据被覆盖的问题。