如何使用 PySimpleGUI 中主 window 中元素的值更新子 window 中的元素?
How do you update an element in a child window with a value from anelement in a main window in PySimpleGUI?
感觉以前解决过这个问题,现在想不通了。我有一个带有两个选项卡的主 window。在其中一个选项卡上,我有一个名称 [-PATIENT-] 的输入元素。退出该元素时,如果输入的名称不在数据库 table 的列表中,则会出现一条消息提醒我这一事实并询问我是否要将新名称添加到数据库 table。如果回复是 'OK',则子 window (add_patient_window) 打开以输入新人的数据以进行保存。为避免错误,我希望子 window 中 ['-NAME-'] 元素中的名称具有选项卡中 ['-PATIENT-] window 的确切值。为此,子 window 'XFER' 中有一个按钮。一切顺利,直到更新 ['-NAME-'] 元素。下面是相关代码。
您会注意到我已经创建了一个函数来从 TAB 中获取 '-PATIENT- 的值并将其格式化为首字母大写。它按预期打印名称。当 'XFER' 按钮在子 window (add_patient_window) 上单击时,我希望它成为 运行 get_new_name() 然后更新 ['-NAME- ] 子 window 中的元素具有从 ['-PATIENT-'] 派生的变量 newname 的值。我已经尝试过函数和事件中的更新,但都不起作用。帮助将不胜感激。
def get_new_name():
newname=values['-PATIENT-'] #This is an element in the main window tab
newname=newname.title()
print('the new patient is :' + newname)
# window['-NAME-'].update(newname) #This element is in the child window
def add_patient_window():
"""
Function to create secondary window
"""
add_patient_layout=[
[sg.Push(),sg.T('Add a patient',text_color='blue',font=40),sg.Push()],
[sg.T('')],
[sg.T('Patient name'),sg.In(size=30, key='-NAME-')],
#[sg.T('Date purchased:'),sg.CalendarButton('Calendar', target='-IN5-', key='_DATE_',format='%Y-%m-%d'),sg.In(key='-IN5-',size=10)],
[sg.T('Patient ID:'),sg.Push(),sg.In(size=10,key='-PATID-')],
[sg.T('Patient date of birth:'),sg.Push(),sg.In(size=10,key='-DOB-')],
[sg.T('Postal address:'),sg.Push(),sg.In(size=10,key='-PADRESS-')],
[sg.T('Street address:'),sg.Push(),sg.In(size=10,key='-ST_ADRESS-')],
[sg.T('Town:'),sg.Push(),sg.In(size=10,key='-TOWN-')],
[sg.T('Post Code:'),sg.Push(),sg.In(size=10,key='-PCODE-')],
[sg.T('Cellphone No:'),sg.Push(),sg.In(size=10,key='-PCELL-')],
[sg.T('Email address:'),sg.Push(),sg.In(size=10,key='-PEMAIL-')],
[sg.T('')],
[sg.Button('Save',size=8,button_color='red',key='-SAVE-'),sg.Push(),sg.Button('Xfer name',size=8,key='-XFER-',button_color='green'),sg.Push(),sg.Button('Quit',size=8,button_color='blue')]
]
add_patient_window=sg.Window('Add a new patient', add_patient_layout, modal=True)#Secondary window
while True: # Event loop for secondary window
event,values=add_patient_window.read() # Read secondary window
if event in (None,'Quit'):
break
elif event == '-SAVE-':
conn=sqlite3.connect(r'/home/bushbug/Databases/FSCashBook')
cur3=conn.cursor()
sql3=("Select Id from Patients Desc")
result=cur3.execute(sql3)
for row in result:
LastId=row[0]
Id=LastId
Id=Id+1
print(Id)
Id=Id
Patient_name=values['-NAME-']
Patient_ID=values['-PATID-']
Patient_DOB=values['-DOB-']
PostAdd=values['-PADRESS-']
StAdd=values['-ST_ADRESS-']
Town=values['-TOWN-']
PostCode=values['-PCODE-']
Cell_phone=values['-PCELL-']
E_mail=values['-PEMAIL-']
print(Id,Patient_name,Patient_ID,Patient_DOB,PostAdd,StAdd,Town,PostCode,Cell_phone,E_mail)
cur4=conn.cursor()
my_patient_data=(Id,Patient_name,Patient_ID,Patient_DOB,PostAdd,StAdd,Town,PostCode,Cell_phone,E_mail)
print(my_patient_data)
Sql4="INSERT INTO Patients (Id,pName,pID,pDOB,pPostAdd,pStAdd,pTown,pPostCode,pCell,pmail) VALUES (?,?,?,?,?,?,?,?,?,?)"
cur4.execute(Sql4,my_patient_data)
conn.commit()
sg.popup('Save','You have successfully saved the record!')
elif event=='-XFER-':
get_new_name()
window['-NAME-'].update(newname) #Element in the child window
add_patient_window.close()
大多数时候,我通过函数参数、全局变量或 class 属性传递值或变量。
下面的代码展示了一个简单的案例
import PySimpleGUI as sg
def popup(username):
username = username.title()
if username == '':
username = "my friend"
sg.theme("DarkBlue4")
layout = [
[sg.Text(f"Nice to meet you, {username} !")],
[sg.Push(), sg.Button('OK')],
]
sg.Window('Welcome', layout, modal=True).read(close=True)
sg.theme('DarkBlue3')
layout = [
[sg.Text("What's your name")],
[sg.Input(key='-NAME-')],
[sg.Button('SEND')],
]
window = sg.Window("test", layout)
while True:
event, values = window.read()
if event in (sg.WINDOW_CLOSED, 'Exit'):
break
elif event == 'SEND':
username = values['-NAME-']
popup(username)
window.close()
感觉以前解决过这个问题,现在想不通了。我有一个带有两个选项卡的主 window。在其中一个选项卡上,我有一个名称 [-PATIENT-] 的输入元素。退出该元素时,如果输入的名称不在数据库 table 的列表中,则会出现一条消息提醒我这一事实并询问我是否要将新名称添加到数据库 table。如果回复是 'OK',则子 window (add_patient_window) 打开以输入新人的数据以进行保存。为避免错误,我希望子 window 中 ['-NAME-'] 元素中的名称具有选项卡中 ['-PATIENT-] window 的确切值。为此,子 window 'XFER' 中有一个按钮。一切顺利,直到更新 ['-NAME-'] 元素。下面是相关代码。 您会注意到我已经创建了一个函数来从 TAB 中获取 '-PATIENT- 的值并将其格式化为首字母大写。它按预期打印名称。当 'XFER' 按钮在子 window (add_patient_window) 上单击时,我希望它成为 运行 get_new_name() 然后更新 ['-NAME- ] 子 window 中的元素具有从 ['-PATIENT-'] 派生的变量 newname 的值。我已经尝试过函数和事件中的更新,但都不起作用。帮助将不胜感激。
def get_new_name():
newname=values['-PATIENT-'] #This is an element in the main window tab
newname=newname.title()
print('the new patient is :' + newname)
# window['-NAME-'].update(newname) #This element is in the child window
def add_patient_window():
"""
Function to create secondary window
"""
add_patient_layout=[
[sg.Push(),sg.T('Add a patient',text_color='blue',font=40),sg.Push()],
[sg.T('')],
[sg.T('Patient name'),sg.In(size=30, key='-NAME-')],
#[sg.T('Date purchased:'),sg.CalendarButton('Calendar', target='-IN5-', key='_DATE_',format='%Y-%m-%d'),sg.In(key='-IN5-',size=10)],
[sg.T('Patient ID:'),sg.Push(),sg.In(size=10,key='-PATID-')],
[sg.T('Patient date of birth:'),sg.Push(),sg.In(size=10,key='-DOB-')],
[sg.T('Postal address:'),sg.Push(),sg.In(size=10,key='-PADRESS-')],
[sg.T('Street address:'),sg.Push(),sg.In(size=10,key='-ST_ADRESS-')],
[sg.T('Town:'),sg.Push(),sg.In(size=10,key='-TOWN-')],
[sg.T('Post Code:'),sg.Push(),sg.In(size=10,key='-PCODE-')],
[sg.T('Cellphone No:'),sg.Push(),sg.In(size=10,key='-PCELL-')],
[sg.T('Email address:'),sg.Push(),sg.In(size=10,key='-PEMAIL-')],
[sg.T('')],
[sg.Button('Save',size=8,button_color='red',key='-SAVE-'),sg.Push(),sg.Button('Xfer name',size=8,key='-XFER-',button_color='green'),sg.Push(),sg.Button('Quit',size=8,button_color='blue')]
]
add_patient_window=sg.Window('Add a new patient', add_patient_layout, modal=True)#Secondary window
while True: # Event loop for secondary window
event,values=add_patient_window.read() # Read secondary window
if event in (None,'Quit'):
break
elif event == '-SAVE-':
conn=sqlite3.connect(r'/home/bushbug/Databases/FSCashBook')
cur3=conn.cursor()
sql3=("Select Id from Patients Desc")
result=cur3.execute(sql3)
for row in result:
LastId=row[0]
Id=LastId
Id=Id+1
print(Id)
Id=Id
Patient_name=values['-NAME-']
Patient_ID=values['-PATID-']
Patient_DOB=values['-DOB-']
PostAdd=values['-PADRESS-']
StAdd=values['-ST_ADRESS-']
Town=values['-TOWN-']
PostCode=values['-PCODE-']
Cell_phone=values['-PCELL-']
E_mail=values['-PEMAIL-']
print(Id,Patient_name,Patient_ID,Patient_DOB,PostAdd,StAdd,Town,PostCode,Cell_phone,E_mail)
cur4=conn.cursor()
my_patient_data=(Id,Patient_name,Patient_ID,Patient_DOB,PostAdd,StAdd,Town,PostCode,Cell_phone,E_mail)
print(my_patient_data)
Sql4="INSERT INTO Patients (Id,pName,pID,pDOB,pPostAdd,pStAdd,pTown,pPostCode,pCell,pmail) VALUES (?,?,?,?,?,?,?,?,?,?)"
cur4.execute(Sql4,my_patient_data)
conn.commit()
sg.popup('Save','You have successfully saved the record!')
elif event=='-XFER-':
get_new_name()
window['-NAME-'].update(newname) #Element in the child window
add_patient_window.close()
大多数时候,我通过函数参数、全局变量或 class 属性传递值或变量。
下面的代码展示了一个简单的案例
import PySimpleGUI as sg
def popup(username):
username = username.title()
if username == '':
username = "my friend"
sg.theme("DarkBlue4")
layout = [
[sg.Text(f"Nice to meet you, {username} !")],
[sg.Push(), sg.Button('OK')],
]
sg.Window('Welcome', layout, modal=True).read(close=True)
sg.theme('DarkBlue3')
layout = [
[sg.Text("What's your name")],
[sg.Input(key='-NAME-')],
[sg.Button('SEND')],
]
window = sg.Window("test", layout)
while True:
event, values = window.read()
if event in (sg.WINDOW_CLOSED, 'Exit'):
break
elif event == 'SEND':
username = values['-NAME-']
popup(username)
window.close()