任何人都可以通过 PySimpleGUI 帮我完成这个代码

Any one help me code finish this by PySimpleGUI

我要做一个程序来教children个乘法器,这是作业的核心代码。

#include <stdio.h>

  #define MIN       1
  #define MAXINDEX  10
  #define MAXTABLE  10
  #define STEP      1

 void main(void)
  {
    int i,j ;

    for (j = MIN; j <MAXTABLE; j += STEP)  {
        for (i = MIN; i <= MAXINDEX; i += STEP)
            printf(“%3d * %3d = %3d\n”, i, j, i*j);
        printf(“\n---------------\n\n”);
    }

    for (i = MIN; i <= MAXINDEX; i += STEP)
        printf(“%3d * %3d = %3d\n”, i, MAXTABLE, i*MAXTABLE);
 }

在我必须将它重写为下一代语言之后,我设计了用 python 编写它。 像这样

j=1
i=1
for j in range(1,11):
    for i in range(1,11):
        print('%3d * %3d =%3d' % (i, j, i *j))
    if j!=10:   
        print("\n---------------\n")
    else:
        print("\n")

然后我必须为它制作一个 UI 所以这张图片就是我设计的 This is the design of me

由于刚学习python3天,所以尽量写下:

import PySimpleGUI as sg
import os.path
def Corecode():
    j=1
    i=1
    for j in range(1,11):
        for i in range(1,11):
            print('%3d * %3d =%3d' % (i, j, i *j))
        if j!=10:   
            print("\n---------------\n")
        else:
            print("\n")
from PySimpleGUI.PySimpleGUI import R, Image, Multiline
showrobot_column = [
    [sg.Image('E:\img\robot.png',size = (300,300))],
],
show_Textbox_selectbox = [
    [sg.Multiline(size=(50,20),key='-def-')],
    [sg.Listbox(values=['a','b','c','d'], enable_events=True, size=(50,20), key="")]

    ]


layout = [
    [sg.Column(showrobot_column),
    sg.VSeparator(),
    sg.Column(show_Textbox_selectbox)
    ]
]
window = sg.Window("First Test",layout)
while True:
    event, value = window.read()
    if event == sg.WIN_CLOSED:
        break
window.close()

我设计成机器人在说话,让children通过按钮进行交互但是,我发现我无法插入图像,而且我找不到打印的方法Multiplier table 由前面的核心代码组成,所以有人可以帮我完成代码或告诉我为什么 sg.image 出错。 TY

试试这个,

import PySimpleGUI as sg


def corecode(base):
    return ("\n"+"-"*13+"\n").join([f'{i+1:>2d} * {base:>2d} = {(i+1)*base:>3d}' for i in range(10)])

sg.theme('DarkBlue3')
sg.set_options(font=("Courier New", 12))

showrobot_column = [
    [sg.Image('E:/img/robot.png', size = (300, 300))],
]

list_values = [f'{i+1:>2d}' for i in range(10)]
show_Textbox_selectbox = [
    [sg.Multiline(size=(15, 20), expand_y=True, key='-MULTILINE-')],
    [sg.Listbox(values=list_values, enable_events=True, size=(15, 10), key="-LISTBOX-")],
]

layout = [
    [sg.Column(showrobot_column),
     sg.VSeparator(),
     sg.Column(show_Textbox_selectbox),],
]

window = sg.Window("First Test", layout, finalize=True)
multiline = window['-MULTILINE-']
multiline.update(value=corecode(1))
listbox = window['-LISTBOX-']
listbox.update(set_to_index=0)
while True:
    event, values = window.read()
    if event == sg.WIN_CLOSED:
        break
    elif event == '-LISTBOX-':
        base = int(values[event][0])
        multiline.update(value=corecode(base))

window.close()

这是对 Jason 回答的无耻小调整(选择他的正确答案)。

希望包含一些可能对某些人有帮助的功能。一是几乎所有元素都有一个初始值,您可以在布局中设置该初始值,而不是先确定它然后再设置它们。它减少了代码。

我还在这个答案中嵌入了一个“小饰品”作为测试。这样您就可以 运行 此 Whosebug 页面上的示例。

import PySimpleGUI as sg


def corecode(base):
    return ("\n"+"-"*13+"\n").join([f'{i+1:>2d} * {base:>2d} = {(i+1)*base:>3d}' for i in range(10)])

sg.theme('DarkBlue3')

list_values = [f'{i+1:>2d}' for i in range(10)]

show_Textbox_selectbox = [[sg.Multiline(default_text=corecode(1), size=(15, 20), expand_y=True, key='-MULTILINE-')],
    [sg.Listbox(values=list_values, default_values=[list_values[0]], enable_events=True, size=(15, 10), key="-LISTBOX-")]]

layout = [
    [sg.Image(sg.EMOJI_BASE64_HAPPY_THUMBS_UP), sg.VSeparator(), sg.Column(show_Textbox_selectbox)]
         ]

window = sg.Window("First Test", layout, font='Courier 12')

while True:
    event, values = window.read()
    if event == sg.WIN_CLOSED:
        break
    elif event == '-LISTBOX-':
        base = int(values[event][0])
        window['-MULTILINE-'].update(value=corecode(base))

window.close()

这是一个 link 小饰品,如果它没有正确嵌入 - https://trinket.io/pygame/d59ef3b352

<iframe src="https://trinket.io/embed/pygame/d59ef3b352" width="100%" height="600" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe>