-2

我正在使用 PySimpleGui 开发一个程序,当我按下复制按钮时,它将复制一个完成和精炼的结果列表到我的剪贴板。到目前为止,它看起来不错,但是当我复制它时,如果值不大于 0,我不想包含字符 G(Goal)、A(Assist) 和 OG(Own Goal)(如果框换句话说,程序是空白的)。现在,当您在下面运行此代码并复制它时,除了 TEAM1 的位置 GK、CDM、MID 和 ST 之外,您还会得到 [G] [A] [OG]。我还没有完成TEAM2。当我弄清楚这一点时会完成它。

所以我想从中得到一些关于我应该做什么的提示,不包括这些字符。我想我可能需要用一些 if 和 else 语句进行一些定义,但考虑到我才 3-4 个月进入 Python,这可能有点复杂。

有人能帮我吗?

[节目图片][1]

import PySimpleGUI as sg
import pyperclip

SYMBOL_UP = '▶'
SYMBOL_DOWN = '❌'

sg.theme_background_color('#1e1e1e')
sg.theme_text_color("#FAFFFF")
sg.theme_input_background_color("#FAFFFF")
sg.theme_button_color("#494949")
sg.theme_text_element_background_color("#1e1e1e")
sg.theme_element_background_color("#1e1e1e")

def collapse(layout, key):
    return sg.pin(sg.Column(layout, key=key))

section1 =[
    [sg.Multiline(key="home_subs", size=(20,3))]
    ]

section2 =[
    [sg.Multiline(key="away_subs", size=(20, 3))]
]

section3 =[
    [sg.Text("EXTRA")]
]

layout = [
    [sg.Text("STATCOUNTER by ClayAces v.1")],
    [sg.Text("------------------")],
    # HOME TEAM
    [sg.Text("HOME TEAM", text_color='#fce784'), sg.Combo(["TEAM1", "TEAM2", "TEAM3","TEAM4","TEAM5","TEAM6"],
        default_value="TEAM1", key="TEAM_1", size=(15,1)), sg.InputText(key="TEAM1_Score", size=(3,1))],
    [sg.Text("GK:"), sg.InputText(key="HomeGK", size=(14,1)), sg.Text("G:"), sg.InputText("", size=(2,1),
        key="homegk_g"), sg.Text("A:"), sg.InputText("", size=(2,1), key="homegk_a"), sg.Text("O.G:"), sg.InputText("", size=(2,1), key="homegk_og")],
    [sg.Text("CDM:"), sg.InputText(key="HomeCDM", size=(14,1)), sg.Text("G:"), sg.InputText("", size=(2,1),
        key="homecdm_g"), sg.Text("A:"), sg.InputText("", size=(2,1), key="homecdm_a"), sg.Text("O.G:"), sg.InputText("", size=(2,1), key="homecdm_og")],
    [sg.Text("MID:"), sg.InputText(key="HomeMID", size=(14,1)), sg.Text("G:"), sg.InputText("", size=(2,1),
        key="homemid_g"), sg.Text("A:"), sg.InputText("", size=(2,1), key="homemid_a"), sg.Text("O.G:"), sg.InputText("", size=(2,1), key="homemid_og")],
    [sg.Text("ST:"), sg.InputText(key="HomeST", size=(14,1)), sg.Text("G:"), sg.InputText("", size=(2,1),
        key="homest_g"), sg.Text("A:"), sg.InputText("", size=(2,1), key="homest_a"), sg.Text("O.G:"), sg.InputText("", size=(2,1), key="homest_og")],
    #  SECTION 1
    [sg.T(SYMBOL_DOWN, enable_events=True, k='-OPEN SEC1-', text_color='#fce784'),
     sg.T('Home subs', enable_events=True, text_color='#fce784', k='-OPEN SEC1-TEXT')],
    [collapse(section1, '-SEC1-')],
    # AWAY TEAM
    [sg.Text("AWAY TEAM", text_color='#fce784'), sg.Combo(["TEAM1", "TEAM2", "TEAM3","TEAM4","TEAM5","TEAM6"],
        default_value="TEAM2", key="TEAM_2", size=(15,1)), sg.InputText(key="TEAM2_Score", size=(3,1))],
    [sg.Text("GK:"), sg.InputText(key="AwayGK", size=(14, 1)), sg.Text("G:"),
     sg.InputText("", size=(2, 1), key="Awaygk_g"), sg.Text("A:"), sg.InputText("", size=(2, 1), key="Awaygk_a"),
     sg.Text("O.G:"), sg.InputText("", size=(2, 1), key="Awaygk_og")],
    [sg.Text("CDM:"), sg.InputText(key="AwayCDM", size=(14, 1)), sg.Text("G:"),
     sg.InputText("", size=(2, 1), key="Awaycdm_g"), sg.Text("A:"), sg.InputText("", size=(2, 1), key="Awaycdm_a"),
     sg.Text("O.G:"), sg.InputText("", size=(2, 1), key="Awaycdm_og")],
    [sg.Text("MID:"), sg.InputText(key="AwayMID", size=(14, 1)), sg.Text("G:"),
     sg.InputText("", size=(2, 1), key="Awaymid_g"), sg.Text("A:"), sg.InputText("", size=(2, 1), key="Awaymid_a"),
     sg.Text("O.G:"), sg.InputText("", size=(2, 1), key="Awaymid_og")],
    [sg.Text("ST:"), sg.InputText(key="AwayST", size=(14, 1)), sg.Text("G:"),
     sg.InputText("", size=(2, 1), key="Awayst_g"), sg.Text("A:"), sg.InputText("", size=(2, 1), key="Awayst_a"),
     sg.Text("O.G:"), sg.InputText("", size=(2, 1), key="Awayst_og")],
    #  SECTION 2
    [sg.T(SYMBOL_DOWN, enable_events=True, k='-OPEN SEC2-', text_color='#fce784'),
     sg.T('Away subs', enable_events=True, text_color='#fce784', k='-OPEN SEC2-TEXT')],
    [collapse(section2, '-SEC2-')],
    # RESULTS
    [sg.Button("COPY RESULTS", key="Copy")],
    #  SECTION 3
    [sg.T(SYMBOL_DOWN, enable_events=True, k='-OPEN SEC3-', text_color='#fce784'),
     sg.T('Extras', enable_events=True, text_color='#fce784', k='-OPEN SEC3-TEXT')],
    [collapse(section3, '-SEC3-')],
]
window = sg.Window("STATCOUNTER v.1", layout, font="Calibri, 11", element_padding=(3, 3),element_justification='r')

opened1, opened2, opened3 = True, True, True

while True:
    event, values = window.read()
    print(event, values)
    if event == sg.WIN_CLOSED or event == 'Exit':
        break

    if event.startswith('-OPEN SEC1-'):
        opened1 = not opened1
        window['-OPEN SEC1-'].update(SYMBOL_DOWN if opened1 else SYMBOL_UP)
        window['-SEC1-'].update(visible=opened1)

    if event.startswith('-OPEN SEC2-'):
            opened2 = not opened2
            window['-OPEN SEC2-'].update(SYMBOL_DOWN if opened2 else SYMBOL_UP)
            window['-SEC2-'].update(visible=opened2)

    if event.startswith('-OPEN SEC3-'):
            opened3 = not opened3
            window['-OPEN SEC3-'].update(SYMBOL_DOWN if opened3 else SYMBOL_UP)
            window['-SEC3-'].update(visible=opened3)

    if event == "Copy":
        pyperclip.copy(f"""
=-=-=-=-=-=-=-==-=-=-=-=-=-=-=
{'TEAM1'} {values['TEAM1_Score']}-{values['TEAM2_Score']} {'TEAM2'}

{'TEAM1'}

GK: {values['HomeGK']} [{values['homegk_g']}]G [{values['homegk_a']}A] [{values['homegk_og']}O.G]
CDM: {values['HomeCDM']} [{values['homecdm_g']}G] [{values['homecdm_a']}A] [{values['homecdm_og']}O.G]
MID: {values['HomeMID']} [{values['homemid_g']}G] [{values['homemid_a']}A] [{values['homemid_og']}O.G]
ST:{values['HomeST']} [{values['homest_g']}G] [{values['homest_a']}A] [{values['homest_og']}O.G]

SUBS:
{values['home_subs']}

{'TEAM2'}

GK: {values['AwayGK']}
CDM: {values['AwayCDM']}
MID: {values['AwayMID']}
ST: {values['AwayST']}

SUBS:
{values['away_subs']}
=-=-=-=-=-=-=-==-=-=-=-=-=-=-=
""") ```


  [1]: https://i.stack.imgur.com/BDpKB.png
4

2 回答 2

0

您只需要在复制之前检查用户输入。

首先,您需要检索该值。例如:

input_data = values['homegk_g']

然后你需要检查它的值,如果它等于一个空字符串(这意味着用户是否输入了一些东西),如下所示:

if input_data != "":
    # rest of the script

检查用户是否输入了某些数据的另一种方法是比较 的,len()如下所示:InputText0

if len(input_data) > 0:
    # rest of the script

现在,如果我想通过用户输入检查显示您的脚本示例,它将是这样的:

if event == "Copy":

    # copy this for other desired fields. 
    input_data_hm_gk_g = values['homegk_g']
    input_data_hm_gk_a = values['homegk_a']
    input_data_hm_gk_og = values['homegk_og']
    if len(input_data_hm_gk) > 0 and len(input_data_hm_gk_a) > 0 and 
                                        len(input_data_hm_gk_og) > 0:


        pyperclip.copy(f"""
        =-=-=-=-=-=-=-==-=-=-=-=-=-=-=
        {'TEAM1'} {values['TEAM1_Score']}-{values['TEAM2_Score']} {'TEAM2'}

        {'TEAM1'}

        GK: {values['HomeGK']} [{values['homegk_g']}]G [{values['homegk_a']}A] 
        [{values['homegk_og']}O.G]
        CDM: {values['HomeCDM']} [{values['homecdm_g']}G] 
        [{values['homecdm_a']}A] 
        [{values['homecdm_og']}O.G]
        MID: {values['HomeMID']} [{values['homemid_g']}G] 
        [{values['homemid_a']}A] 
        [{values['homemid_og']}O.G]
        ST:{values['HomeST']} [{values['homest_g']}G] [{values['homest_a']}A] 
        [{values['homest_og']}O.G]

        SUBS:
        {values['home_subs']}

        {'TEAM2'}

        GK: {values['AwayGK']}
        CDM: {values['AwayCDM']}
        MID: {values['AwayMID']}
        ST: {values['AwayST']}

        SUBS:
        {values['away_subs']}
        =-=-=-=-=-=-=-==-=-=-=-=-=-=-=
        """)

您有很多输入字段要检查,因此我建议您编写一个单独的函数来检查是否所有输入字段都已填写。你可以简单地通过迭代values和我上面告诉你的那些条件检查来做到这一点。

于 2022-01-08T23:09:52.247 回答
0

(我是python新手,所以如果没有帮助请不喜欢)你为什么不使用'>'

'>' 表示左边的值高于右边的值。

例子

new_list = [] 
for item in list:
  if item > 0:
    new_list.append(item)
    
于 2022-01-08T21:32:10.890 回答