如何使用单选按钮将设置的整数输入变量?

发布于 2024-12-09 22:53:55 字数 1424 浏览 0 评论 0原文

我使用下面的代码行在多项选择命令行软件中输入“BAI_var1”变量。

BAI_var1 = input ("Please enter 0 for not at all, 1 for somewhat, 2 for moderatly or 3 for a lot:")

这是我的图形用户界面:

from Tkinter import *
import time

    class App:
        def __init__(self, master):
            w = Label(master, text="1. Anxiety, nervousness, worry or fear")
            w.pack()

            v = IntVar()
            Radiobutton(master, text="0 for not at all", variable=v, value=1).pack(side=TOP, anchor="w")
            Radiobutton(master, text="1 for somewhat", variable=v, value=2).pack(side=TOP, anchor="w")
            Radiobutton(master, text="2 for moderatly", variable=v, value=3).pack(side=TOP, anchor="w")
            Radiobutton(master, text="3 for a lot", variable=v, value=4).pack(side=TOP, anchor="w")

            self.button = Button(master, text="BACK", fg="red", command=self.button6)
            self.button.pack(side=BOTTOM)
            self.button = Button(master, text="NEXT", fg="red", command=self.button5)
            self.button.pack(side=BOTTOM)


        def button6(self):
            print "Sam is awesome!GAJONGA" 

        def button5(self):
            print "PYTHON FOR THE WIN! GIAN SAYS PYTHON = FILTHY" 

    master = Tk()
    app = App(master)
    master.mainloop()

我的问题是我不知道如何使用单选按钮将设置的整数输入到变量。

I have used the line of code below to input a variable for "BAI_var1" in a multiple choice command line software.

BAI_var1 = input ("Please enter 0 for not at all, 1 for somewhat, 2 for moderatly or 3 for a lot:")

Here is my Graphic User Interface:

from Tkinter import *
import time

    class App:
        def __init__(self, master):
            w = Label(master, text="1. Anxiety, nervousness, worry or fear")
            w.pack()

            v = IntVar()
            Radiobutton(master, text="0 for not at all", variable=v, value=1).pack(side=TOP, anchor="w")
            Radiobutton(master, text="1 for somewhat", variable=v, value=2).pack(side=TOP, anchor="w")
            Radiobutton(master, text="2 for moderatly", variable=v, value=3).pack(side=TOP, anchor="w")
            Radiobutton(master, text="3 for a lot", variable=v, value=4).pack(side=TOP, anchor="w")

            self.button = Button(master, text="BACK", fg="red", command=self.button6)
            self.button.pack(side=BOTTOM)
            self.button = Button(master, text="NEXT", fg="red", command=self.button5)
            self.button.pack(side=BOTTOM)


        def button6(self):
            print "Sam is awesome!GAJONGA" 

        def button5(self):
            print "PYTHON FOR THE WIN! GIAN SAYS PYTHON = FILTHY" 

    master = Tk()
    app = App(master)
    master.mainloop()

My problem is that I don't know how to use a Radiobutton to input a set integer to a variable.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

晒暮凉 2024-12-16 22:53:55

使用字典将整数值映射到它们表示的字符串:

choices = {
    1: "not at all",
    2: "somewhat",
    3: "moderately",
    4: "a lot"
}

然后您可以使用它来创建 UI:

for (i in sorted(choices.keys())):
    label = "%s - %s" % (i, choices[i])
    rb=Radiobutton(master, text=label, variable=v, value=i)
    rb.pack(side=TOP, anchor="w")

当您需要获取值时,显然这只是使用变量的值作为键的简单字典查找。

Use a dictionary to map the integer values to the strings they represent:

choices = {
    1: "not at all",
    2: "somewhat",
    3: "moderately",
    4: "a lot"
}

You can then use this to create the UI:

for (i in sorted(choices.keys())):
    label = "%s - %s" % (i, choices[i])
    rb=Radiobutton(master, text=label, variable=v, value=i)
    rb.pack(side=TOP, anchor="w")

When you need to fetch the values, obviously it's just a simple dictionary lookup using the value of the variable as the key.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文