【问题标题】:How do I have my code run for every row and not only for the first one?如何让我的代码运行在每一行,而不仅仅是第一行?
【发布时间】:2020-09-08 05:27:18
【问题描述】:
import PySimpleGUI as sg

#sg.theme('DarkBrown7')

layout = [

    [sg.Text('Medico Refertatore', size=(15, 1)), sg.InputText()],
    [sg.Text('Nome Paziente', size=(15, 1)), sg.InputText()],
    [sg.Text('Cognome Paziente', size=(15, 1)), sg.InputText()],
    [sg.Text('j', size=(15, 1)), sg.Button('0', focus = True ), sg.Button('1-24%', focus = True), sg.Button('25-49%', focus = True), sg.Button('50-69%', focus = True), sg.Button('70-99%', focus = True), sg.Button('100%', focus = True), ],
    [sg.Text('a', size=(15, 1)), sg.Button('0', focus = True ), sg.Button('1-24%', focus = True), sg.Button('25-49%', focus = True), sg.Button('50-69%', focus = True), sg.Button('70-99%', focus = True), sg.Button('100%', focus = True), ],
    [sg.Text('b', size=(15, 1)), sg.Button('0', focus = True ), sg.Button('1-24%', focus = True), sg.Button('25-49%', focus = True), sg.Button('50-69%', focus = True), sg.Button('70-99%', focus = True), sg.Button('100%', focus = True), ],
    [sg.Text('c', size=(15, 1)), sg.Button('0', focus = True ), sg.Button('1-24%', focus = True), sg.Button('25-49%', focus = True), sg.Button('50-69%', focus = True), sg.Button('70-99%', focus = True), sg.Button('100%', focus = True), ],
    [sg.Text('d', size=(15, 1)), sg.Button('0', focus = True ), sg.Button('1-24%', focus = True), sg.Button('25-49%', focus = True), sg.Button('50-69%', focus = True), sg.Button('70-99%', focus = True), sg.Button('100%', focus = True), ],
    [sg.Text('e', size=(15, 1)), sg.Button('0', focus = True ), sg.Button('1-24%', focus = True), sg.Button('25-49%', focus = True), sg.Button('50-69%', focus = True), sg.Button('70-99%', focus = True), sg.Button('100%', focus = True), ],
    [sg.Text('f', size=(15, 1)), sg.Button('0', focus = True ), sg.Button('1-24%', focus = True), sg.Button('25-49%', focus = True), sg.Button('50-69%', focus = True), sg.Button('70-99%', focus = True), sg.Button('100%', focus = True), ],
    [sg.Text('g', size=(15, 1)), sg.Button('0', focus = True ), sg.Button('1-24%', focus = True), sg.Button('25-49%', focus = True), sg.Button('50-69%', focus = True), sg.Button('70-99%', focus = True), sg.Button('100%', focus = True), ],
    [sg.Text('h', size=(15, 1)), sg.Button('0', focus = True ), sg.Button('1-24%', focus = True), sg.Button('25-49%', focus = True), sg.Button('50-69%', focus = True), sg.Button('70-99%', focus = True), sg.Button('100%', focus = True), ],
    [sg.Text('i', size=(15, 1)), sg.Button('0', focus = True ), sg.Button('1-24%', focus = True), sg.Button('25-49%', focus = True), sg.Button('50-69%', focus = True), sg.Button('70-99%', focus = True), sg.Button('100%', focus = True), ],
    [sg.Text('l', size=(15, 1)), sg.Button('0', focus = True ), sg.Button('1-24%', focus = True), sg.Button('25-49%', focus = True), sg.Button('50-69%', focus = True), sg.Button('70-99%', focus = True), sg.Button('100%', focus = True), ],
    [sg.Text('m', size=(15, 1)), sg.Button('0', focus = True ), sg.Button('1-24%', focus = True), sg.Button('25-49%', focus = True), sg.Button('50-69%', focus = True), sg.Button('70-99%', focus = True), sg.Button('100%', focus = True), ],
    [sg.Text('n', size=(15, 1)), sg.Button('0', focus = True ), sg.Button('1-24%', focus = True), sg.Button('25-49%', focus = True), sg.Button('50-69%', focus = True), sg.Button('70-99%', focus = True), sg.Button('100%', focus = True), ],
    [sg.Text('o', size=(15, 1)), sg.Button('0', focus = True ), sg.Button('1-24%', focus = True), sg.Button('25-49%', focus = True), sg.Button('50-69%', focus = True), sg.Button('70-99%', focus = True), sg.Button('100%', focus = True), ],
    [sg.Text('p', size=(15, 1)), sg.Button('0', focus = True ), sg.Button('1-24%', focus = True), sg.Button('25-49%', focus = True), sg.Button('50-69%', focus = True), sg.Button('70-99%', focus = True), sg.Button('100%', focus = True), ],
    [sg.Text('q', size=(15, 1)), sg.Button('0', focus = True ), sg.Button('1-24%', focus = True), sg.Button('25-49%', focus = True), sg.Button('50-69%', focus = True), sg.Button('70-99%', focus = True), sg.Button('100%', focus = True), ],
    [sg.Text('r', size=(15, 1)), sg.Button('0', focus = True ), sg.Button('1-24%', focus = True), sg.Button('25-49%', focus = True), sg.Button('50-69%', focus = True), sg.Button('70-99%', focus = True), sg.Button('100%', focus = True), ],
    [sg.Text('s', size=(15, 1)), sg.Button('0', focus = True ), sg.Button('1-24%', focus = True), sg.Button('25-49%', focus = True), sg.Button('50-69%', focus = True), sg.Button('70-99%', focus = True), sg.Button('100%', focus = True), ],
    [sg.Text('t', size=(15, 1)), sg.Button('0', focus = True ), sg.Button('1-24%', focus = True), sg.Button('25-49%', focus = True), sg.Button('50-69%', focus = True), sg.Button('70-99%', focus = True), sg.Button('100%', focus = True), ],
    [sg.Text('u', size=(15, 1)), sg.Button('0', focus = True ), sg.Button('1-24%', focus = True), sg.Button('25-49%', focus = True), sg.Button('50-69%', focus = True), sg.Button('70-99%', focus = True), sg.Button('100%', focus = True), ],
    [sg.Text('v', size=(15, 1)), sg.Button('0', focus = True ), sg.Button('1-24%', focus = True), sg.Button('25-49%', focus = True), sg.Button('50-69%', focus = True), sg.Button('70-99%', focus = True), sg.Button('100%', focus = True), ],
    [sg.Text('z', size=(15, 1)), sg.Button('0', focus = True ), sg.Button('1-24%', focus = True), sg.Button('25-49%', focus = True), sg.Button('50-69%', focus = True), sg.Button('70-99%', focus = True), sg.Button('100%', focus = True), ],
    [sg.Submit(), sg.Cancel()]

]

window = sg.Window('APP RICERCA', layout)

while True:
    event, values = window.read()

    if event in (None,'Cancel'):   # if user closes window or clicks cancel
        break

    elif event == '0':
        print('1')
    elif event == '1-24%':
        print('2')  
    elif event == '25-49%':
        print('3')
    elif event == '50-69%':
        print('4') 
    elif event == '70-99%':
        print('5')      
    elif event == '100%':
        print('6')



window.close()

我在上面写了这段代码,但我无法让它在每一行都运行。只为第一个。我将如何以及如何解决这个问题?

【问题讨论】:

    标签: python user-interface interface data-entry pysimplegui


    【解决方案1】:

    PySimpleGUI在重复的对象名称末尾添加了一个后缀,这就是==不起作用的原因,将其替换为.startswith()如下:

    import PySimpleGUI as sg
    
    sg.theme('DarkBrown7')
    layout = [
    
    [sg.Text('Medico Refertatore', size=(15, 1)), sg.InputText()],
    [sg.Text('Nome Paziente', size=(15, 1)), sg.InputText()],
    [sg.Text('Cognome Paziente', size=(15, 1)), sg.InputText()],
    [sg.Text('j', size=(15, 1)), sg.Button('0', focus = True ), sg.Button('1-24%', focus = True), sg.Button('25-49%', focus = True), sg.Button('50-69%', focus = True), sg.Button('70-99%', focus = True), sg.Button('100%', focus = True), ],
    ...
    
    [sg.Text('z', size=(15, 1)), sg.Button('0', focus = True ), sg.Button('1-24%', focus = True), sg.Button('25-49%', focus = True), sg.Button('50-69%', focus = True), sg.Button('70-99%', focus = True), sg.Button('100%', focus = True), ],
    [sg.Submit(), sg.Cancel()]
    ]
    
    window = sg.Window('APP RICERCA', layout)
    
    while True:
        event, values = window.read()    
        if event in (None,'Cancel'):   # if user closes window or clicks cancel
            break
    
        elif event.startswith('0'):
            print('1')
        elif event.startswith('1-24%'):
            print('2')  
        elif event.startswith('25-49%'):
            print('3')
        elif event.startswith('50-69%'):
            print('4') 
        elif event.startswith('70-99%'):
            print('5')      
        elif event.startswith('100%'):
            print('6')
        else:
            print(event)
    window.close()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-22
      • 1970-01-01
      • 2020-09-07
      • 2019-02-01
      • 1970-01-01
      • 2022-10-06
      相关资源
      最近更新 更多