如果值不大于 0 或空白,如何从打印中排除字符
How to exclude characters from a print if value is not greater than 0 or blank
我正在使用 PySimpleGui 开发一个程序,当我按下复制按钮时,它将把完成和优化的结果列表复制到我的剪贴板。到目前为止,它看起来不错,但是当我复制它时,如果值不大于 0 (换句话说,如果程序中的框为空白)。现在,当你运行这个代码,你复制它,你会得到[G][A][O.G]除了TEAM1位置GK,CDM,MID和ST。我还没有完成 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
(我是 python 的新手所以如果没有帮助请不喜欢)
你为什么不使用'>'
'>'表示左边的值比右边的大
例子
new_list = []
for item in list:
if item > 0:
new_list.append(item)
您只需在复制之前检查用户输入即可。
首先您需要检索值。例如:
input_data = values['homegk_g']
然后你需要检查它的值,如果它等于一个空字符串(这意味着用户输入了一些东西)如下:
if input_data != "":
# rest of the script
检查用户是否输入了某些数据的另一种方法是比较 InputText
的 len()
和 0
,如下所示:
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
和我上面告诉你的那些条件检查来做到这一点。
我正在使用 PySimpleGui 开发一个程序,当我按下复制按钮时,它将把完成和优化的结果列表复制到我的剪贴板。到目前为止,它看起来不错,但是当我复制它时,如果值不大于 0 (换句话说,如果程序中的框为空白)。现在,当你运行这个代码,你复制它,你会得到[G][A][O.G]除了TEAM1位置GK,CDM,MID和ST。我还没有完成 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
(我是 python 的新手所以如果没有帮助请不喜欢) 你为什么不使用'>'
'>'表示左边的值比右边的大
例子
new_list = []
for item in list:
if item > 0:
new_list.append(item)
您只需在复制之前检查用户输入即可。
首先您需要检索值。例如:
input_data = values['homegk_g']
然后你需要检查它的值,如果它等于一个空字符串(这意味着用户输入了一些东西)如下:
if input_data != "":
# rest of the script
检查用户是否输入了某些数据的另一种方法是比较 InputText
的 len()
和 0
,如下所示:
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
和我上面告诉你的那些条件检查来做到这一点。