简体   繁体   English

我在使用第一个 gui 应用程序中的按钮时遇到问题

[英]I am having trouble with a button in my first gui application

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()

I am trying to change the question using the button nbut , and it won't work.我正在尝试使用按钮nbut更改问题,但它不起作用。 I tried using randint outside the function and passing it but then the button works only once我尝试在randint之外使用 randint 并传递它但是按钮只工作一次

Change改变

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

To this.为此。

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

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

The explanation is simple.解释很简单。

You are not calling the nxt function when nbut click Instead you are calling the lambda function. But, because you put the function name inside the lambda function without parentheses the nxt function never executes.nbut单击时,您没有调用nxt function 相反,您调用的是lambda function。但是,因为您将 function 名称放在 lambda function 中,没有括号nxt从不执行 never888989

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我无法将GUI插入到我的python程序中 - I am having trouble incorparating a GUI into my python program 我在读取此csv文件并将第一列命名为“ team”的代码时遇到麻烦 - I am having trouble with my code reading this csv file and naming the first column “team” 我的播放器类中的 stat 函数有问题 - I am having trouble with my stat function in my player class 我在使用或导入matplotlib时遇到问题。 这是我的代码的第一行:将matplotlib.pyplot导入为plt - I am having trouble using or importing matplotlib. This is the first line of my code: import matplotlib.pyplot as plt 关于比较我的两个词典时遇到的麻烦的建议? - Suggestions on the trouble I am having with comparing my two dictionaries? 我无法从 python 表单获取输出 - I am having trouble getting outputs from my python form 我在使用 PyGame 中的平台游戏时遇到碰撞问题 - I am having trouble with getting collisions to work with my platformer in PyGame 我无法让我的函数返回全局变量 - I am having trouble getting my function to return a global varriable 我这里的代码有问题吗? 我对结果有问题 - Is there any problem with my code here? I am having trouble with the result 我在退出pygame中的无限循环时遇到问题 - I am having trouble exiting my infinite loop in pygame
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM