【问题标题】:Grid of buttons in two columns using pysimplegui使用 pysimplegui 的两列按钮网格
【发布时间】:2023-01-31 00:21:41
【问题描述】:

编程游戏的新手,正在寻找一些建议。我可以在 pysimplegui 中生成一个 10 * 10 的按钮网格没问题,但我希望在我的布局中设置 2 个并排的网格。

import PySimpleGUI as sg

sg.theme('Dark Blue 3')
MAX_ROWS = MAX_COL = 10


layout =   [[sg.Text('menu', font='Default 25')],
           [sg.Text(size=(15,1), key='-MESSAGE-', font='Default 20')]]

layout +=  [[sg.Button(str('O'), size=(4, 2), pad=(0,0), border_width=0, key=(row,col)) for col in range(MAX_COL)] for row in range(MAX_ROWS)]

layout +=  [[sg.Button('Exit', button_color=('white', 'red'))]]

window = sg.Window('menu', layout)

while True:         # The Event Loop
        event, values = window.read()
        print(event, values)
        if event in (sg.WIN_CLOSED, 'Exit'):
            break
        if event in position:           # simulate a hit or a miss
            window[event].update('H', button_color=('white','red'))
            window['-MESSAGE-'].update('Hit')
        else:
            window[event].update('M', button_color=('white','black'))
            window['-MESSAGE-'].update('Miss')
window.close()

任何建议。

我的基本布局如下,只是希望在原始布局旁边的第二列中重复它。

当我尝试将网格添加到列视图时,我不断收到错误消息,说网格被视为列表而不是元素。

【问题讨论】:

    标签: python


    【解决方案1】:

    使用函数生成两个相同的布局,但对相同的元素使用不同的键。

    import PySimpleGUI as sg
    
    def grids(index):
        return [[sg.Button(str('O'), size=(4, 2), pad=(0,0), border_width=0, key=(index, row,col)) for col in range(MAX_COL)] for row in range(MAX_ROWS)]
    
    sg.theme('Dark Blue 3')
    MAX_ROWS = MAX_COL = 10
    layout = [[sg.Column(grids(0)), sg.Column(grids(1))]]
    sg.Window('Title', layout).read(close=True)
    

    【讨论】:

      猜你喜欢
      • 2022-10-16
      • 2019-08-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-18
      相关资源
      最近更新 更多