如何使用PySimpleGui从列表创建单选按钮? [英] How can I create radio buttons from a list using PySimpleGui?

查看:1228
本文介绍了如何使用PySimpleGui从列表创建单选按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用PySimpleGui从列表中动态创建单选按钮,但是我在布局代码中插入循环的努力正在捕获语法错误.可以使用API​​来完成此操作,还是需要使用tkinter使其退出?我的列表是通过对网络驱动器进行有针对性的文件搜索生成的.

I want use PySimpleGui to dynamically create radio buttons from a list, but my efforts to insert a loop in the layout code are catching syntax errors. Can this be done with the API or do I need to slog it out with tkinter? My list is being generated by a targeted file search of a network drive.

我尝试串联布局",将单选按钮部分置于for循环中.还尝试在[sg.Radio()]声明本身中插入for循环.都行不通.

I've tried concatenating 'layout', putting the radio button section in a for loop. Also attempted to insert a for loop in the [sg.Radio()] declaration itself. Neither works.

import PySimpleGUI as sg

xList = ['a', 'b', ... 'zz']

layout = [[sg.Text('Select a thingy')],
          [sg.Radio(<for thingy in xList: 'thingy', thingy>)],
                   #^^^^^^ for loop is psuedo code
          [sg.OK(), sg.Cancel()]]

推荐答案

我认为这就是您想要的?

I think this is what you're looking for?

import PySimpleGUI as sg

radio_choices = ['a', 'b', 'c']
layout = [
            [sg.Text('My layout')],
            [sg.Radio(text, 1) for text in radio_choices],
            [sg.Button('Read')]
         ]

window = sg.Window('Radio Button Example', layout)

while True:             # Event Loop
    event, values = window.Read()
    if event is None:
        break
    print(event, values)

它将产生以下窗口:

有多种构建" layout变量的方法.以下是产生相同窗口的其他几种组合:

There are a number of ways of "building" a layout variable. Here are a couple of other combinations that produce the same window:

这是第一个,每次一次构建一行,然后最后将它们添加在一起

This first one builds one row at a time and then adds them together in the end

# Build Layout
top_part = [[sg.Text('My layout')]]
radio_buttons = [[sg.Radio(x,1) for x in radio_choices]]
read = [[sg.Button('Read')]]
layout = top_part + radio_buttons + read

此代码也一次生成一个单行,然后将它们添加在一起,但是它是在单个语句而不是4中执行的.

This one also builds a single row at a time and then adds them together, but it does it in a single statement instead of 4.

   # Build layout
    layout = [[sg.Text('My layout')]] + \
                [[sg.Radio(text, 1) for text in radio_choices]] + \
                [[sg.Button('Read')]]


如果您想每行添加一个按钮,那么也有几种方法可以做到这一点.如果您使用的是Python 3.6,那么它将起作用:


If you wanted to add these buttons one per line, then there are several ways of doing this too. If you are using Python 3.6, then this will work:

layout = [
            [sg.Text('My layout')],
            *[[sg.Radio(text, 1),] for text in radio_choices],
            [sg.Button('Read')]
         ]

构建布局"技术将在上述*运算符无效的系统上运行.

The "Build a layout" technique will work on systems where the above * operator is not valid.

radio_choices = ['a', 'b', 'c']
radio = [[sg.Radio(text, 1),] for text in radio_choices]
layout = [[sg.Text('My layout')]] + radio + [[sg.OK()]]

将这些变体与窗口代码和事件循环结合使用时,将产生一个如下所示的窗口:

Both of these variations when combined with the window code and the event loop, will produce a window that looks like this:

这篇关于如何使用PySimpleGui从列表创建单选按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆