风und桥 2022-05-16 16:35 采纳率: 86.4%
浏览 59
已结题

python中改一个代码

我做了一个答题程序

img

img


我想问下面这张图片中框起来的程序要怎样才能换成再下一张图片中的程序?(原本的程序是题目我需要自己一个个输进去,后面的程序可以从题库中随机抽题)

img

img



import tkinter
import tkinter.messagebox
import sqlite3


def lgsql():  # 链接数据库
    entryname = varname.get()
    entrypass = varpass.get()
    conn = sqlite3.connect('users.sqlite')
    SQL = "SELECT * FROM user WHERE name='%s'" % entryname
    cur = conn.execute(SQL)
    lst = cur.fetchall()
    conn.commit()
    if lst:
        if entrypass == lst[0][3]:#什么意思来着的
            tkinter.messagebox.showinfo('欢迎', '欢迎' + entryname + '! 您的成绩将计入您的账户')
        else:
            tkinter.messagebox.showerror('错误', '密码错误!')
    else:
        tkinter.messagebox.showerror('错误', '没有这个用户!')
    conn.close()


def regist2():  # 注册 直接链接到数据库
    entryname = varname.get()
    entrypass = varpass.get()
    conn = sqlite3.connect('users.sqlite')
    SQL = ''' insert into user(name,true,false,passwd)
            values('%s','%s','%s','%s')''' % (entryname, '', '', entrypass)
    conn.execute(SQL)
    tkinter.messagebox.showinfo('成功', '注册成功!')
    conn.commit()
    conn.close()


def question(event):
    winnew = tkinter.Toplevel(root)
    winnew.geometry("200x200+200+200")
    winnew.title("选题窗口")
    btnhaigui = tkinter.Button(winnew, text='海龟', width=8)
    btnhaigui.place(x=20, y=20)
    btnhaitun = tkinter.Button(winnew, text='海豚', width=8)
    btnhaitun.place(x=120, y=20)
    btnjingyu = tkinter.Button(winnew, text='鲸鱼', width=8)
    btnjingyu.place(x=20, y=60)
    btnshayu = tkinter.Button(winnew, text='鲨鱼', width=8)
    btnshayu.place(x=120, y=60)
    btnshuimu = tkinter.Button(winnew, text='水母', width=8)
    btnshuimu.place(x=20, y=100)
    btnxia = tkinter.Button(winnew, text='虾', width=8)
    btnxia.place(x=120, y=100)
    btclose = tkinter.Button(winnew, text="关闭页面", width=16, command=winnew.destroy)
    btclose.place(x=40, y=140)

    btnhaigui.bind("<Button-1>", haigui)
    btnhaitun.bind("<Button-1>", haitun)
    btnjingyu.bind("<Button-1>", jingyu)
    btnshayu.bind("<Button-1>", shayu)
    btnshuimu.bind("<Button-1>", shuimu)
    btnxia.bind("<Button-1>", xia)


def haigui(event):
    winnew1 = tkinter.Toplevel(root)



 winnew1.geometry("800x400+200+200")
    winnew1.title("海龟")
    lbl11 = tkinter.Label(winnew1, text='1.龟类属于爬行动物,全世界种类很多,有400多种。这些龟仅有8种栖于海洋,称为海龟,下列哪些龟不是海龟()')
    lbl11.place(x=20, y=20)

    def showselect():
        varlbl2.set(varR.get())

    varR = tkinter.StringVar()
    varR.set("Y")
    r1 = tkinter.Radiobutton(winnew1, text="A. 玳瑁", value="A",variable=varR,command=showselect)
    r1.place(x=40,y=70)

    r2 = tkinter.Radiobutton(winnew1, text="B. 棱皮龟", value="B",variable=varR,command=showselect)
    r2.place(x=200,y=70)

    r3 = tkinter.Radiobutton(winnew1, text="C. 山龟", value="C",variable=varR,command=showselect)
    r3.place(x=40,y=100)

    r4 = tkinter.Radiobutton(winnew1, text="D. 俪龟", value="D",variable=varR,command=showselect)
    r4.place(x=200,y=100)

    lbl11 = tkinter.Label(winnew1, text="你选了:")
    lbl11.place(x=40,y=250)

    varlbl2=tkinter.StringVar()
    lbl12 = tkinter.Label(winnew1,textvariable=varlbl2)
    lbl12.place(x=100,y=250)

    def answer():
        if lbl12 == "C":#why??
            tkinter.messagebox.showinfo('正确', '回答正确!')
        else:
            tkinter.messagebox.showerror('错误', '回答错误!')

    btnanswer = tkinter.Button(winnew1,text="下一题",command=answer)
    btnanswer.place(x=700,y=300)

  • 写回答

2条回答 默认 最新

  • 关注

    你题目的解答代码如下:

    import tkinter
    import tkinter.messagebox
    import sqlite3
    import pandas as pd
    import random
    
    def lgsql():  # 链接数据库
        entryname = varname.get()
        entrypass = varpass.get()
        conn = sqlite3.connect('users.sqlite')
        SQL = "SELECT * FROM user WHERE name='%s'" % entryname
        cur = conn.execute(SQL)
        lst = cur.fetchall()
        conn.commit()
        if lst:
            if entrypass == lst[0][3]:#什么意思来着的
                tkinter.messagebox.showinfo('欢迎', '欢迎' + entryname + '! 您的成绩将计入您的账户')
            else:
                tkinter.messagebox.showerror('错误', '密码错误!')
        else:
            tkinter.messagebox.showerror('错误', '没有这个用户!')
        conn.close()
    
    
    def regist2():  # 注册 直接链接到数据库
        entryname = varname.get()
        entrypass = varpass.get()
        conn = sqlite3.connect('users.sqlite')
        SQL = ''' insert into user(name,true,false,passwd)
                values('%s','%s','%s','%s')''' % (entryname, '', '', entrypass)
        conn.execute(SQL)
        tkinter.messagebox.showinfo('成功', '注册成功!')
        conn.commit()
        conn.close()
    
    
    def question(event):
        winnew = tkinter.Toplevel(root)
        winnew.geometry("200x200+200+200")
        winnew.title("选题窗口")
        btnhaigui = tkinter.Button(winnew, text='海龟', width=8)
        btnhaigui.place(x=20, y=20)
        btnhaitun = tkinter.Button(winnew, text='海豚', width=8)
        btnhaitun.place(x=120, y=20)
        btnjingyu = tkinter.Button(winnew, text='鲸鱼', width=8)
        btnjingyu.place(x=20, y=60)
        btnshayu = tkinter.Button(winnew, text='鲨鱼', width=8)
        btnshayu.place(x=120, y=60)
        btnshuimu = tkinter.Button(winnew, text='水母', width=8)
        btnshuimu.place(x=20, y=100)
        btnxia = tkinter.Button(winnew, text='虾', width=8)
        btnxia.place(x=120, y=100)
        btclose = tkinter.Button(winnew, text="关闭页面", width=16, command=winnew.destroy)
        btclose.place(x=40, y=140)
    
        btnhaigui.bind("<Button-1>", haigui)
        btnhaitun.bind("<Button-1>", haitun)
        btnjingyu.bind("<Button-1>", jingyu)
        btnshayu.bind("<Button-1>", shayu)
        btnshuimu.bind("<Button-1>", shuimu)
        btnxia.bind("<Button-1>", xia)
    
    
    def haigui(event):
        ex_data = pd.read_excel('题目.xlsx') ##默认读取sheet = 0的
        head_list = list(ex_data.columns)
        list_dict = []
        for i in ex_data.values:
            list_dict.append(dict(zip(head_list,i)))
        print(list_dict)
        drt = random.choice(list_dict)
        print(drt['序号'])
    
        winnew1 = tkinter.Toplevel(root)
        winnew1.geometry("800x400+200+200")
        winnew1.title("海龟")
        lbl11 = tkinter.Label(winnew1, text=f"{drt['序号']}.{drt['题目']}")
        lbl11.place(x=20, y=20)
    
        def showselect():
            varlbl2.set(varR.get())
    
        varR = tkinter.StringVar()
        varR.set("Y")
        r1 = tkinter.Radiobutton(winnew1, text=f"A. {drt['选项A']}", value="A",variable=varR,command=showselect)
        r1.place(x=40,y=70)
    
        r2 = tkinter.Radiobutton(winnew1, text=f"B. {drt['选项B']}", value="B",variable=varR,command=showselect)
        r2.place(x=200,y=70)
    
        r3 = tkinter.Radiobutton(winnew1, text=f"C. {drt['选项C']}", value="C",variable=varR,command=showselect)
        r3.place(x=40,y=100)
    
        r4 = tkinter.Radiobutton(winnew1, text=f"D. {drt['选项D']}", value="D",variable=varR,command=showselect)
        r4.place(x=200,y=100)
    
        lbl11 = tkinter.Label(winnew1, text="你选了:")
        lbl11.place(x=40,y=250)
    
        varlbl2=tkinter.StringVar()
        lbl12 = tkinter.Label(winnew1,textvariable=varlbl2)
        lbl12.place(x=100,y=250)
    
        def answer():
            if varlbl2.get() == drt['正确选项']:
                tkinter.messagebox.showinfo('正确', '回答正确!')
            else:
                tkinter.messagebox.showerror('错误', '回答错误!')
    
        btnanswer = tkinter.Button(winnew1,text="下一题",command=answer)
        btnanswer.place(x=700,y=300)
    
    
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 5月24日
  • 已采纳回答 5月16日
  • 修改了问题 5月16日
  • 修改了问题 5月16日
  • 展开全部

悬赏问题

  • ¥15 onlyoffice编辑完后立即下载,下载的不是最新编辑的文档
  • ¥15 求caverdock使用教程
  • ¥15 Coze智能助手搭建过程中的问题请教
  • ¥15 12864只亮屏 不显示汉字
  • ¥20 三极管1000倍放大电路
  • ¥15 vscode报错如何解决
  • ¥15 前端vue CryptoJS Aes CBC加密后端java解密
  • ¥15 python随机森林对两个excel表格读取,shap报错
  • ¥15 基于STM32心率血氧监测(OLED显示)相关代码运行成功后烧录成功OLED显示屏不显示的原因是什么
  • ¥100 X轴为分离变量(因子变量),如何控制X轴每个分类变量的长度。