Tkinter单选按钮实现

huangapple go评论69阅读模式
英文:

Tkinter radiobutton implementation

问题

以下是您提供的代码的翻译部分:

我的第一个问题简单来说我想要使用tkinter实现一个包含多组单选按钮的界面每个组都有三个单选按钮普通)。在代码执行后用户将选择他想要的单选按钮然后在最后按下一个按钮以收集所有单选按钮的值这样我们可以得到一个统计视图看看有多少个比如说被选中了尽量使上述所有内容都尽可能动态化

我尝试过的方法
我使用生成器来处理以下内容
1. 标签的行
2. 单选按钮的行
3. 变量var应该采用的名称

以下是代码

# 请注意,这只是代码的翻译部分。如果您有任何问题或需要进一步的帮助,请随时提出。

```python
import tkinter as tk
from tkinter import ttk

my_list = ["Test 1", "Test 2", "Test 3"]

# 设置UI的所有组件。
root = tk.Tk()
root.title('单选按钮测试')
root.geometry("1024x768")

# 为放置单选按钮创建计数器。
# 通用计数器,用于标签或可能有用的地方。
def counter(listfile):
    i = 0
    for i in range(len(listfile) * 3 + 1):
        if i != 0:
            yield i
            i += 1
count = counter(my_list)

# 用于具有偶数行号的行的计数器。
def even_counter(listfile):
    for i in range(len(listfile) * 2 + 1):
        if i != 0 and i % 2 == 0:
            yield i
even = even_counter(my_list)

# 用于具有奇数行号的行的计数器。
def odd_counter(listfile):
    odd_numbers = []
    for i in range(len(listfile) * 3 + 1):
        if i > 2 and i % 2 != 0:
            odd_numbers.append(i)
    odd_lst = odd_numbers * 4
    odd_lst.sort()
    for x in odd_lst:
        yield x
odd = odd_counter(my_list)

# 生成要用于单选按钮变量命名的数字。
def var_values(listfile):
    var_numbers = []
    for i in range(len(listfile) * 3 + 1):
        var_numbers.append(i)
    var_lst = var_numbers * 3
    var_lst.sort()
    for x in var_lst:
        yield x
rb_var_values = var_values(my_list)

def labels(listfile):
    for i in listfile:
        label_text = tk.Label(root, text=str(next(count)) + '. ' + i, font=("Arial", 11))
        label_text.grid(column=0, row=next(even), sticky="w", columnspan=3)

# 生成单选按钮。
rblst = []
var_value = 0
def radio_buttons(listfile):
    global var
    for rb in range(len(listfile)):
        var = tk.IntVar(None)
        var_value = next(rb_var_values)
        var.set(var_value)
        rb1 = ttk.Radiobutton(root, text="好", variable=var, value=1)
        rb2 = ttk.Radiobutton(root, text="普通", variable=var, value=2)
        rb3 = ttk.Radiobutton(root, text="差", variable=var, value=3)
        rblst.extend([rb1, rb2, rb3])
        comment = tk.Entry(root, width=60, borderwidth=3)
        rb1.grid(column=0, row=next(odd))
        rb2.grid(column=1, row=next(odd))
        rb3.grid(column=2, row=next(odd))
        comment.grid(column=3, row=next(odd), pady=5)

# "统计"按钮的命令函数。
def stat_btn():
    for i in rblst:
        print(f'ID是:{id(i)},变量是{var.get()}')
    print('长度为', len(rblst))

# 创建并放置"统计"按钮。
bt = tk.Button(root, text="统计", command=stat_btn)
bt.grid(column=5, row=47, sticky="se")

labels(my_list)
var_values(my_list)
radio_buttons(my_list)
root.mainloop()

按下“统计”按钮后,我得到了错误的结果,这意味着只有最后一组单选按钮会影响var.get的结果,而不是第一组和第二组。您认为是变量还是单选按钮的值导致了这个问题?以上代码的面向对象实现是否会使事情更容易管理?

英文:

My first ever question in here. To the point. I want to implement an interface with tkinter containing multiple groups of radio buttons. Every group will have three radio buttons (Good, Normal, Bad). After the code execution the user will choose the radio buttons he wants and at the end he presses a button to collect the values of all radio buttons so we could have a statistic view of how many, let's say "Good", have been selected. All the above should be implemented as much dynamically as it is possible.

The things i tried.
I used generators for the:

  1. Rows of the labels.
  2. Rows of the radio buttons.
  3. Names that the variable=var should take.

Here's the code.

import tkinter as tk
from tkinter import ttk
my_list = ["Test 1", "Test 2", "Test 3"]
# Set up all the components of the UI.
root = tk.Tk()
root.title('Radio Buttons Test')
root.geometry("1024x768")
# Create counters for use in placing radio buttons.
# Generic counter for labels or wherever it might be useful.
def counter(listfile):
i = 0
for i in range(len(listfile) * 3 + 1):
if i != 0:
yield i
i +=1
count = counter(my_list)
#Counter for rows with even numbers.
def even_counter(listfile):
for i in range(len(listfile) * 2 + 1):
if i != 0 and i % 2 == 0:
yield i
even = even_counter(my_list)
#Counter for rows with odd numbers.
def odd_counter(listfile):
odd_numbers = []
for i in range(len(listfile) * 3 + 1):
if i > 2 and i % 2 != 0:
odd_numbers.append(i)
odd_lst = odd_numbers * 4
odd_lst.sort()
for x in odd_lst:
yield x
odd = odd_counter(my_list)
# Generating numbers to be used in radiobutton's variable naming.
def var_values(listfile):
var_numbers = []
for i in range(len(listfile) * 3 + 1):
var_numbers.append(i)
var_lst = var_numbers * 3
var_lst.sort()
for x in var_lst:
yield x
rb_var_values = var_values(my_list)
def labels(listfile):
for i in listfile:
label_text = tk.Label(root, text=str(next(count))+ '. ' + i, font=("Arial", 11))
label_text.grid(column=0, row=next(even), sticky="w", columnspan=3)
# Generate radio buttons.
rblst = []
var_value = 0
def radio_buttons(listfile):
global var
for rb in range(len(listfile)):
var = tk.IntVar(None)
var_value = next(rb_var_values)
var.set(var_value)
rb1 = ttk.Radiobutton(root, text="Good", variable=var, value=1)
rb2 = ttk.Radiobutton(root, text="Normal", variable=var, value=2)
rb3 = ttk.Radiobutton(root, text="Bad", variable=var, value=3)
rblst.extend([rb1,rb2,rb3])
comment = tk.Entry(root, width=60, borderwidth=3)
rb1.grid(column=0, row=next(odd))
rb2.grid(column=1, row=next(odd))
rb3.grid(column=2, row=next(odd))
comment.grid(column=3, row=next(odd), pady=5)
# Function for the command of "Stats" button.
def stat_btn():
for i in rblst:
print(f'ID is: {id(i)} and Variable is {var.get()}')
print('Length is',len(rblst))
# Create and place the "Stats" button.
bt = tk.Button(root, text="Stats", command=stat_btn)
bt.grid(column=5,row=47, sticky="se")
labels(my_list)
var_values(my_list)
radio_buttons(my_list)
root.mainloop()

By pressing the Stats button i get wrong results, meaning that only the last group of radio buttons affects the result of var.getand not the first and the second group. You think it's the variable or the value of the radio buttons that cause this problem? Will OOP implementation of the above code will make things easier to manage?

答案1

得分: 2

import tkinter as tk
from tkinter import ttk

my_list = ["Test 1", "Test 2", "Test 3"]

root = tk.Tk()
root.title('Radio Buttons Test')
root.geometry("1024x768")

def counter(listfile):
    i = 0
    for i in range(len(listfile) * 3 + 1):
        if i != 0:
            yield i
            i += 1

count = counter(my_list)

def even_counter(listfile):
    for i in range(len(listfile) * 2 + 1):
        if i != 0 and i % 2 == 0:
            yield i

even = even_counter(my_list)

def odd_counter(listfile):
    odd_numbers = []
    for i in range(len(listfile) * 3 + 1):
        if i > 2 and i % 2 != 0:
            odd_numbers.append(i)
    odd_lst = odd_numbers * 4
    odd_lst.sort()
    for x in odd_lst:
        yield x

odd = odd_counter(my_list)

def var_values(listfile):
    var_numbers = []
    for i in range(len(listfile) * 3 + 1):
        var_numbers.append(i)
    var_lst = var_numbers * 3
    var_lst.sort()
    for x in var_lst:
        yield x

rb_var_values = var_values(my_list)

def labels(listfile):
    for i in listfile:
        label_text = tk.Label(root, text=str(next(count))+ '. ' + i, font=("Arial", 11))
        label_text.grid(column=0, row=next(even), sticky="w", columnspan=3)

rblst = []
var_values_list = []  # List to hold the var variables for each group

def radio_buttons(listfile):
    global var
    for rb in range(len(listfile)):
        var = tk.IntVar(None)
        var_value = next(rb_var_values)
        var.set(var_value)
        rb1 = ttk.Radiobutton(root, text="Good", variable=var, value=1)
        rb2 = ttk.Radiobutton(root, text="Normal", variable=var, value=2)
        rb3 = ttk.Radiobutton(root, text="Bad", variable=var, value=3)
        rblst.extend([rb1, rb2, rb3])
        comment = tk.Entry(root, width=60, borderwidth=3)
        rb1.grid(column=0, row=next(odd))
        rb2.grid(column=1, row=next(odd))
        rb3.grid(column=2, row=next(odd))
        comment.grid(column=3, row=next(odd), pady=5)
        var_values_list.append(var)  # Store the var variable for each group

def stat_btn():
    for i, var in enumerate(var_values_list):
        print(f'ID is: {id(rblst[i])} and Variable is {var.get()}')
    print('Length is', len(rblst))

bt = tk.Button(root, text="Stats", command=stat_btn)
bt.grid(column=5, row=47, sticky="se")

labels(my_list)
var_values(my_list)
radio_buttons(my_list)
root.mainloop()
英文:

You can implement like this, You can copy this code and i hope this works.

import tkinter as tk
from tkinter import ttk
my_list = ["Test 1", "Test 2", "Test 3"]
root = tk.Tk()
root.title('Radio Buttons Test')
root.geometry("1024x768")
def counter(listfile):
i = 0
for i in range(len(listfile) * 3 + 1):
if i != 0:
yield i
i +=1
count = counter(my_list)
def even_counter(listfile):
for i in range(len(listfile) * 2 + 1):
if i != 0 and i % 2 == 0:
yield i
even = even_counter(my_list)
def odd_counter(listfile):
odd_numbers = []
for i in range(len(listfile) * 3 + 1):
if i > 2 and i % 2 != 0:
odd_numbers.append(i)
odd_lst = odd_numbers * 4
odd_lst.sort()
for x in odd_lst:
yield x
odd = odd_counter(my_list)
def var_values(listfile):
var_numbers = []
for i in range(len(listfile) * 3 + 1):
var_numbers.append(i)
var_lst = var_numbers * 3
var_lst.sort()
for x in var_lst:
yield x
rb_var_values = var_values(my_list)
def labels(listfile):
for i in listfile:
label_text = tk.Label(root, text=str(next(count))+ '. ' + i, font=("Arial", 11))
label_text.grid(column=0, row=next(even), sticky="w", columnspan=3)
rblst = []
var_values_list = []  # List to hold the var variables for each group
def radio_buttons(listfile):
global var
for rb in range(len(listfile)):
var = tk.IntVar(None)
var_value = next(rb_var_values)
var.set(var_value)
rb1 = ttk.Radiobutton(root, text="Good", variable=var, value=1)
rb2 = ttk.Radiobutton(root, text="Normal", variable=var, value=2)
rb3 = ttk.Radiobutton(root, text="Bad", variable=var, value=3)
rblst.extend([rb1, rb2, rb3])
comment = tk.Entry(root, width=60, borderwidth=3)
rb1.grid(column=0, row=next(odd))
rb2.grid(column=1, row=next(odd))
rb3.grid(column=2, row=next(odd))
comment.grid(column=3, row=next(odd), pady=5)
var_values_list.append(var)  # Store the var variable for each group
def stat_btn():
for i, var in enumerate(var_values_list):
print(f'ID is: {id(rblst[i])} and Variable is {var.get()}')
print('Length is', len(rblst))
bt = tk.Button(root, text="Stats", command=stat_btn)
bt.grid(column=5, row=47, sticky="se")
labels(my_list)
var_values(my_list)
radio_buttons(my_list)
root.mainloop()

答案2

得分: 1

是的,它起作用了!我卡在这里两个星期了...我的错在于我没有将“var”值收集到一个列表中,这样我以后就可以再次访问它们。感谢你的帮助。

英文:

Yes it works! I got stuck on this for two weeks... My fault was that i didn't collect the "var" values in a list so i could access them again later. Glad for your help.

huangapple
  • 本文由 发表于 2023年7月17日 17:00:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/76702893.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定