鱼C论坛

 找回密码
 立即注册
查看: 2420|回复: 2

请大神看一下这个代码处理随机数那边该怎么修改

[复制链接]
发表于 2022-12-30 17:43:05 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
处理随机数的那边出错,看不出问题,求大神帮忙
import tkinter as tk
import time
import random

f = open('info.txt', 'r', encoding='utf-8')
fi = f.readlines()
dic = {}
for lst in fi:
    d = str(lst).strip('\n').split(",")
    print(d)
    dic[d[0]] = d[1]
    print(d[0])
    print(d[1])

# 销毁登录窗口,创建主窗口,子函数嵌套完成
def pop_window():
    root.destroy()
    win = tk.Tk()
    win.title("MainBox")
    win.geometry("350x500")
    win.resizable(0,0)
    win.iconbitmap('favicon.ico')
    win.tk.call("source", "azure.tcl")
    win.tk.call("set_theme", "dark")

    varCount = tk.StringVar(root, value='')
    entryCount = tk.Entry(win, width=10,textvariable=varCount)
    entryCount.place(x=20, y=20, height=50)

    num = tk.StringVar()

    # 处理随机数函数
    i=0
    _num = tk.StringVar()

    def _randnum():
        #global i = 0
        count = entryCount.get()
        print(count)
        num.set(str(random.sample(range(1,60),int(count))))
        _num = num[i]
        i += 1
        return _num

    # 绘制窗口组件

    labelNumber = tk.Label(win,textvariable=_num,font=("Arial",48))
    labelNumber.place(x=75,y=70)
    buttonClick = tk.Button(win,text="Start",font=("Arial",18),command=_randnum)
    buttonClick.place(x=20,y=200,width=100,height=60)

    # 启动消息循环
    win.update()
    win.minsize(win.winfo_width(), win.winfo_height())
    x_cordinate = int((win.winfo_screenwidth() / 2) - (win.winfo_width() / 2))
    y_cordinate = int((win.winfo_screenheight() / 2) - (win.winfo_height() / 2))
    win.geometry("+{}+{}".format(x_cordinate, y_cordinate - 20))
    win.mainloop()
    #登录窗口后销毁自身窗口呼出win窗口进行工作

# 登录事件处理函数
def login():
    # 获取用户名和密码
        buttonCancel['state'] = 'disable'
        buttonOk['state'] = 'disable'
        name = entryName.get()
        pwd = entryPwd.get()
        print(name)
        print(pwd)
        def OK():
            root['height']=300
        # 删除控件
            labelPrompt.destroy()
            labelMessage.destroy()
            buttonOk['state']='active'
            buttonCancel['state']='active'

        if name in dic and dic[name] == pwd:
            labelMessage = tk.Label(root,
                                    text='Message',
                                    fg='white',
                                    bg='blue',
                                    font=("Times New Roman",16),
                                    justify=tk.RIGHT,
                                    anchor='w',
                                    width=80)
            labelMessage.place(x=0,y=200,width=600,height=30)
            root['height']=350
            labelPrompt = tk.Label(root,
                                   text='登陆成功',
                                   font=("微软雅黑",14),
                                   justify=tk.RIGHT,
                                   anchor='e',
                                   width=80)
            labelPrompt.place(x=90,y=250,width=90,height=30)
            buttonOkk = tk.Button(root,
                                  text='ok',
                                  font=("微软雅黑",14),
                                  activebackground='#ff0000',
                                  command=OK)
            buttonOkk.place(x=500, y=300, width=80, height=30)

            pop_window()

        else:
            labelMessage = tk.Label(root, text="Message                                                              "
                                                    "    请重新输入账号密码       ", fg='white', bg='blue',
                                         font=("Times New Roman", 16),
                                         justify=tk.RIGHT, anchor='w', width=600)
            labelMessage.place(x=0, y=200, width=600, height=30)
            root['height'] = 350
            labelPrompt = tk.Label(root, text='用户名和密码不匹配', font=("微软雅黑", 14), justify=tk.RIGHT,
                                        anchor='w',
                                        width=100)
            labelPrompt.place(x=90, y=250, width=180, height=30)
            buttonOkk = tk.Button(root, text='ok', font=("微软雅黑", 14), activeforeground='#ff0000', command=OK)
            buttonOkk.place(x=500, y=300, width=80, height=30)

# 取消事件处理函数
def cancel():
    # 清除文本框
    varName.set('')
    varPwd.set('')

if __name__ == "__main__":
    #  建立窗口
    root = tk.Tk()
    root.title("LoginBox")
    root.geometry("600x300")
    root.resizable(0, 0)
    root.iconbitmap('favicon.ico')
    root.tk.call("source", "azure.tcl")
    root.tk.call("set_theme", "dark")

    # 在窗口上创建标签组件
    labelTitle = tk.Label(root, text='Login', font=("微软雅黑", 14), justify=tk.CENTER, anchor='center', width=200)
    labelTitle.place(x=210, y=15, width=200, height=30)
    labeName = tk.Label(root, text='Account:', font=("微软雅黑", 12), justify=tk.RIGHT, anchor='e', width=80)
    labeName.place(x=50, y=50, width=125, height=30)
    labeName = tk.Label(root, text='Password:', font=("微软雅黑", 12), justify=tk.RIGHT, anchor='e', width=80)
    labeName.place(x=50, y=90, width=125, height=30)

    # 创建文本框,同时设置相关变量
    varName = tk.StringVar(root, value='')
    entryName = tk.Entry(root, width=80, textvariable=varName)
    entryName.place(x=200, y=50, width=250, height=30)  # 创建密码文本框,同时设置关联的变量
    varPwd = tk.StringVar(root, value='')
    entryPwd = tk.Entry(root, show='*', width=80, textvariable=varPwd)
    entryPwd.place(x=200, y=90, width=250, height=30)

    # 创建Login按钮
    buttonOk = tk.Button(root, text='确定', command=login)
    buttonOk.place(x=200, y=135, width=80, height=30)

    # 创建Cancel按钮
    buttonCancel = tk.Button(root, text='取消', command=cancel)
    buttonCancel.place(x=370, y=135, width=80, height=30)

    # 启动消息循环
    root.update()
    root.minsize(root.winfo_width(), root.winfo_height())
    x_cordinate = int((root.winfo_screenwidth() / 2) - (root.winfo_width() / 2))
    y_cordinate = int((root.winfo_screenheight() / 2) - (root.winfo_height() / 2))
    root.geometry("+{}+{}".format(x_cordinate, y_cordinate - 20))

    root.mainloop()
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-12-30 17:57:15 | 显示全部楼层
请使用代码格式!

1.PNG
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-1-8 09:44:41 | 显示全部楼层
请使用代码格式!
0-1.jpg
0-2.jpg
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-9-25 01:25

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表