你帮我定义一下这个函数,我要求下一个窗口是随机验证码,依然用tkinter来做
时间: 2024-05-12 18:15:48 浏览: 13
好的,以下是一个示例代码,用于生成随机验证码并在下一个窗口中显示。
```python
import random
import tkinter as tk
def generate_code():
"""生成随机验证码"""
code = ''
for i in range(4):
code += chr(random.randint(65, 90)) # 随机生成大写字母
return code
def show_next_window():
"""显示下一个窗口并生成验证码"""
code = generate_code()
# 创建新窗口
next_window = tk.Toplevel(root)
next_window.title('验证码')
next_window.geometry('200x100')
# 将验证码显示在新窗口中
label = tk.Label(next_window, text=code, font=('Arial', 30))
label.pack(pady=20)
# 创建主窗口
root = tk.Tk()
root.title('验证码生成器')
root.geometry('200x100')
# 添加按钮,点击后显示下一个窗口
button = tk.Button(root, text='生成验证码', command=show_next_window)
button.pack(pady=20)
root.mainloop()
```
这个代码中,`generate_code()` 函数用于生成长度为4的随机验证码,其中每个字符都是大写字母。`show_next_window()` 函数则用于创建下一个窗口,并在其中显示生成的验证码。主窗口中添加一个按钮,点击后调用 `show_next_window()` 函数显示下一个窗口。