我制作了一個小條形碼制作 GUI 專案,當“清除”功能發生時,它會洗掉“另存為”按鈕中的文本。所有其他按鈕都沒有問題,我試圖避免為每個鍵放置一行代碼來清除。在將代碼粘貼到此處之前,我對代碼進行了一些細微的更改,因此我知道現在if len(values['Area']) > 0: window.perform_long_operation(lambda: get_barcode(),'Saved Pop')需要更新,這始終是正確的。我忘記了當我添加組合框并設定默認值時,這將是真的。另外,如果有人能告訴我為什么我必須在上面有“lambda”,盡管我沒有傳遞引數?
更新 10:45 CT - 我編譯了程式并且程式沒有正確保存/根本沒有保存。如果我輸入像“test1”這樣的名稱,它應該在我指定的位置保存為 test1.pdf。當我轉到該檔案不存在的位置時,但如果我搜索 C 驅動器,它會顯示,但它幾乎無法定位,就好像它在創建時被洗掉一樣。這僅在我編譯程式時發生。
import PySimpleGUI as sg
import string
pixel_size = [64,128,192,256]
tab1_layout = [
[sg.Text('Area Letter:', size=(28,1)), sg.Combo(list(string.ascii_uppercase[:]), default_value= 'A',key='Area')],
[sg.Text('Start Number Range:', size=(28,1)), sg.InputText(key='start_number')],
[sg.Text('End Number Range:', size=(28,1)), sg.InputText(key='end_number')],
]
tab2_layout = [
[sg.Text('Single Barcode ABC123:', size=(28,1)), sg.InputText(key='single_bc')],
[sg.Text('Number of Single Barcode Replicas:', size=(28,1)), sg.InputText(default_text= 1,key='number_of_barcodes')],
]
layout = [
[sg.Text('Barcode Font Size in Pixels'), sg.Combo(pixel_size, default_value=64, s=(15,22), enable_events=True, readonly=True, key='PIXEL')],
[sg.TabGroup([[sg.Tab('Range of Barcodes', tab1_layout), sg.Tab('Single Barcodes', tab2_layout)]])],
[[sg.Input(key = 'input_loc'), sg.SaveAs(target= 'input_loc' , default_extension= '.pdf')]],
[sg.Submit(),sg.Button('Clear'), sg.Exit(),]
]
def get_barcode():
print('get barcode')
window = sg.Window('Barcode Maker', layout, font=("Helvetica", 12))
def clear_input():
for key in values:
window[key]('')
window['Area']('A')
window['PIXEL'](64)
window['number_of_barcodes'](1)
return None
while True:
event, values = window.read(timeout = 10)
if event == sg.WIN_CLOSED or event == 'Exit':
break
if event == 'Clear':
clear_input()
if event == 'Submit':
save_location = values['input_loc']
if len(values['Area']) > 0:
window.perform_long_operation(lambda: get_barcode(),'Saved Pop')
elif len(values['single_bc']) >=1:
bc_size = values['PIXEL']
single_code = values['single_bc'].upper()
replicas = values['number_of_barcodes']
replicas = int(replicas)
start_rep = 0
while start_rep < replicas:
layout_borb1.add(Barcode(data=single_code, type=BarcodeType.CODE_128, width=Decimal(bc_size), height=Decimal(bc_size),))
with open(save_location, 'wb') as pdf_file_handle:
PDF.dumps(pdf_file_handle, doucment)
start_rep =1
clear_input()```
uj5u.com熱心網友回復:
函式中的以下代碼clear_input清除了所有帶有 in 鍵的元素的值values。
for key in values:
window[key]('')
在valuesnot only Input、 elements 、Comboalso和elements 中 element 的鍵。TabTabGroupButton
>>> values
{'PIXEL': 64, 'Area': 'A', 'start_number': '', 'end_number': '', 'single_bc': '', 'number_of_barcodes': '1', 0: 'Range of Barcodes', 'input_loc': '', 'Save As...': ''}
其中的鍵values包括 button 的鍵Save As...,這就是為什么該按鈕的文本也被清除的原因。
應該有一個規則來指定要清除哪些元素,例如
for key, element in window.key_dict.items():
if isinstance(element, (sg.Input, sg.Combo)):
element.update(value='')
用于lambda定義不帶引數的函式與函式名幾乎相同。
window.perform_long_operation(get_barcode,'Saved Pop')
如果可能,將您的代碼減少到僅包含相關問題和可執行代碼,例如
import PySimpleGUI as sg
import string
pixel_size = [64,128,192,256]
tab1_layout = [
[sg.Text('Area Letter:', size=(28,1)), sg.Combo(list(string.ascii_uppercase[:]), default_value= 'A',key='Area')],
[sg.Text('Start Number Range:', size=(28,1)), sg.InputText(key='start_number')],
[sg.Text('End Number Range:', size=(28,1)), sg.InputText(key='end_number')],
]
tab2_layout = [
[sg.Text('Single Barcode ABC123:', size=(28,1)), sg.InputText(key='single_bc')],
[sg.Text('Number of Single Barcode Replicas:', size=(28,1)), sg.InputText(default_text= 1,key='number_of_barcodes')],
]
layout = [
[sg.Text('Barcode Font Size in Pixels'), sg.Combo(pixel_size, default_value=64, s=(15,22), enable_events=True, readonly=True, key='PIXEL')],
[sg.TabGroup([[sg.Tab('Range of Barcodes', tab1_layout), sg.Tab('Single Barcodes', tab2_layout)]])],
[[sg.Input(key = 'input_loc'), sg.SaveAs(target= 'input_loc' , default_extension= '.pdf')]],
[sg.Submit(),sg.Button('Clear'), sg.Exit(),]
]
def get_barcode():
print("get bar_code function called")
window = sg.Window('Barcode Maker', layout, font=("Helvetica", 12))
def clear_input():
for key, element in window.key_dict.items():
if isinstance(element, (sg.Input, sg.Combo)):
element.update(value='')
window['Area']('A')
window['PIXEL'](64)
window['number_of_barcodes'](1)
return
while True:
event, values = window.read()
if event == sg.WIN_CLOSED or event == 'Exit':
break
if event == 'Clear':
clear_input()
elif event == 'Submit':
save_location = values['input_loc']
if len(values['Area']) > 0:
window.perform_long_operation(get_barcode,'Saved Pop')
clear_input()
elif event == 'Saved Pop':
print("get_barcode complete")
window.close()
對于某些條件,如element.taget==(None, None)或element.Key is not None,“選擇器”按鈕將保存values從回傳的字典中的選擇資訊window.read()。
具有以下任何一項的“選擇器”按鈕button_type:
- BUTTON_TYPE_COLOR_CHOOSER
- BUTTON_TYPE_SAVEAS_FILE
- BUTTON_TYPE_BROWSE_FILE
- BUTTON_TYPE_BROWSE_FILES
- BUTTON_TYPE_BROWSE_FOLDER
- BUTTON_TYPE_CALENDAR_CHOOSER
函式SaveAs定義為 Button 元素file_types=FILE_TYPES_ALL_FILES。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/478042.html
上一篇:如何使用pandas在Python中編輯.csv檔案(追加行和洗掉行)
下一篇:Python行程等待
