PySimple GUI에서 버튼 클릭을 기반으로 다른 레이아웃을 관리 할 수있는 방법이 있는지 궁금합니다. 이 프레임 워크를 사용하기 시작했으며 메뉴를 탐색하는 가장 좋은 방법을 찾고 싶습니다. 다른 레이아웃을 사용할 필요는 없지만이 작업을 수행하는 가장 직관적 인 방법에 놀랐습니다.
특정 하위 메뉴 버튼을 선택하면 맨 위로 밀리는 레이아웃 목록이있는 것처럼 생각했습니다.
layouts = [layout1, layout2, layout3, layout4]
또는 다음으로 프로그램을 시작하십시오.
layout = layout1
하위 메뉴가 선택되면 상태를 다음으로 변경하십시오.
layout = layout2
예를 들어 '메인 메뉴'레이아웃이 있고 버튼을 클릭하면 다른 레이아웃 또는 '하위 메뉴'를 '앞쪽'으로 가져와 전체 프로그램이 하나의 창에서 실행되도록합니다. 아마도 다음과 같이 보일 수 있습니다.
메인 메뉴
버튼 1
버튼 2
버튼 3
버튼 1을 클릭하면 창이 열려 있지만 디스플레이는 하위 메뉴 1로 변경됩니다.
PySimpleGui 문서에서 일부 프로그램을 구성하는 데 권장되는 영구 창 루프를 사용하고 있습니다.
import PySimpleGUI as sg
sg.theme('BluePurple')
layout = [[sg.Text('Your typed chars appear here:'), sg.Text(size=(15,1), key='-OUTPUT-')],
[sg.Input(key='-IN-')],
[sg.Button('Show'), sg.Button('Exit')]]
window = sg.Window('Pattern 2B', layout)
while True: # Event Loop
event, values = window.read()
print(event, values)
if event in (None, 'Exit'):
break
if event == 'Show':
# Update the "output" text element to be the value of "input" element
window['-OUTPUT-'].update(values['-IN-'])
window.close()
구조를 완전히 변경할 수 있지만 기능 구축을 시작하기 전에 메뉴 탐색을 아래로 내리고 싶었습니다.
당신은 실제로 매우 가깝습니다.
여기에 당신이 찾고 있다고 생각하는 것이 있습니다. 해야 할 일은 레이아웃을 Column 요소에 추가하는 것입니다. 그런 다음 표시하려는 열을 제외하고 모든 열을 보이지 않게 만듭니다.
이것은 좋은 생각입니다.
import PySimpleGUI as sg
# ----------- Create the 3 layouts this Window will display -----------
layout1 = [[sg.Text('This is layout 1 - It is all Checkboxes')],
*[[sg.CB(f'Checkbox {i}')] for i in range(5)]]
layout2 = [[sg.Text('This is layout 2')],
[sg.Input(key='-IN-')],
[sg.Input(key='-IN2-')]]
layout3 = [[sg.Text('This is layout 3 - It is all Radio Buttons')],
*[[sg.R(f'Radio {i}', 1)] for i in range(8)]]
# ----------- Create actual layout using Columns and a row of Buttons
layout = [[sg.Column(layout1, key='-COL1-'), sg.Column(layout2, visible=False, key='-COL2-'), sg.Column(layout3, visible=False, key='-COL3-')],
[sg.Button('Cycle Layout'), sg.Button('1'), sg.Button('2'), sg.Button('3'), sg.Button('Exit')]]
window = sg.Window('Swapping the contents of a window', layout)
layout = 1 # The currently visible layout
while True:
event, values = window.read()
print(event, values)
if event in (None, 'Exit'):
break
if event == 'Cycle Layout':
window[f'-COL{layout}-'].update(visible=False)
layout = layout + 1 if layout < 3 else 1
window[f'-COL{layout}-'].update(visible=True)
elif event in '123':
window[f'-COL{layout}-'].update(visible=False)
layout = int(event)
window[f'-COL{layout}-'].update(visible=True)
window.close()
[편집] 새 데모 프로그램이 "Demo_Column_Elem_Swap_Entire_Window.py"라는 PySimpleGUI GitHub에 추가되었습니다. Trinket 을 방문하여 코드를보고 브라우저에서 실행할 수 있습니다 .
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다