python 我的第一个gui应用程序中的某个按钮出现问题

xzv2uavs  于 2023-01-08  发布在  Python
关注(0)|答案(1)|浏览(133)
def play():
    wind2 = tk.Toplevel()
    v = tk.IntVar()
    ques = ["Identify the least stable ion amongst the following.",
            "The set representing the correct order of first ionisation potential is",
            "The correct order of radii is"]
    o1 = ["", "Li⁺", "Be⁻", "B⁻", "C⁻"]
    o2 = ["", "K > Na > Li", "Be > Mg >Ca", "B >C > N", "Ge > Si >C"]
    o3 = ["", "N < Be < B", "F⁻ < O²⁻ < N³⁻", "Na < Li < K", "Fe³⁺ < Fe⁴⁺ < Fe²⁺"]
    choice=[o1, o2, o3]
    qsn = tk.Label(wind2, text = ques[0])
    qsn.pack()

    

    def correct():
        selected=v.get()
        print(selected)
        
    r1 = tk.Radiobutton(wind2, text = o1[1], variable = v, value = 1, command=correct)
    r2 = tk.Radiobutton(wind2, text = o1[2], variable = v, value = 2, command=correct)
    r3 = tk.Radiobutton(wind2, text = o1[3], variable = v, value = 3, command=correct)
    r4 = tk.Radiobutton(wind2, text = o1[4], variable = v, value = 4, command=correct)
    r1.pack()
    r2.pack()
    r3.pack()
    r4.pack()

    def nxt():
        n = random.randint(1,2)
        qsn['text'] = ques[n]
        r1['text'] = choice[n][1]
        r2['text'] = choice[n][2]
        r3['text'] = choice[n][3]
        r4['text'] = choice[n][4]
    nbut = tk.Button(wind2, text = "next", command = lambda: nxt)
    nbut.pack()

我尝试使用按钮nbut更改问题,但它不起作用。我尝试在函数外部使用randint并传递它,但按钮只起作用一次

xbp102n0

xbp102n01#

变更

nbut = tk.Button(wind2, text = "next", command = lambda: nxt)

为了这个。

nbut = tk.Button(wind2, text = "next", command = nxt) # Recommended
## OR

# nbut = tk.Button(wind2, text = "next", command = lambda: nxt())

原因很简单。
nbut单击时,您不是在调用nxt函数,而是在调用lambda函数。但是,由于您将函数名放在lambda函数中,而没有使用括号,因此nxt函数永远不会执行。

相关问题