我可以根据pysimplegui中的用户操作使特定的行可见

发布于 2025-01-21 16:36:38 字数 1492 浏览 0 评论 0原文

我正在使用Pysimplegui创建一个基本的待办事项计划。我只想在用户运行程序时仅显示第一行。我写了接下来的4行,但已将可见性设置为false。当用户单击“添加任务”时,我希望下一行出现。如果再次单击,我希望第三行出现,这应该适用于所有行。有办法这样做吗?

这是到目前为止的代码:

import datetime

#Formatting date and time
x = datetime.datetime.now()
y = x.strftime("%x")
z = x.strftime("%A")

i = 0
def add_line():
    i += 1


prac1 = [
    [sg.Text(f"Welcome to the To-Do-List for {z}, {y}")],
    [sg.Text('1.'),sg.InputText('',key = 'line1'),sg.Checkbox('',key='c1',visible = False)],
    [sg.Text('2.', visible = False),sg.InputText('',key = 'line2', visible = False),sg.Checkbox('',key='c2',visible = False)],
    [sg.Text('3.', visible = False),sg.InputText('',key = 'line3', visible = False),sg.Checkbox('',key='c3',visible = False)],
    [sg.Text('4.', visible = False),sg.InputText('',key = 'line4', visible = False),sg.Checkbox('',key='c4',visible = False)],
    [sg.Text('5.', visible = False),sg.InputText('',key = 'line5', visible = False),sg.Checkbox('',key='c5',visible = False)],
    [sg.Button('+ Add task')],
    [sg.Button('Save'), sg.Button('Exit')]
]

prac2 = [
    [sg.Text('world')],
    [sg.Submit()]]

layout =  [
        [sg.Column(prac1),
        sg.VSeparator(),
        sg.Column(prac2)]]

window = sg.Window('To-Do-List',layout)
while True:
    event, values = window.read()
    if event == sg.WIN_CLOSED or event == 'Exit':
        break
    elif event == '+ Add task':
        """How can i use sg.window[].update here to make lines apear sequentially?"""```

I am creating a basic to-do-list program using PySimpleGUI. I want only the first line to be shown when the user runs the program. I have written the next 4 lines, but have set the visibility to False. When user clicks 'Add Task', I want the next line to appear. If clicked again, I want the third line to appear and this should work for all lines. Is there a way to do this?

Here is the code so far:

import datetime

#Formatting date and time
x = datetime.datetime.now()
y = x.strftime("%x")
z = x.strftime("%A")

i = 0
def add_line():
    i += 1


prac1 = [
    [sg.Text(f"Welcome to the To-Do-List for {z}, {y}")],
    [sg.Text('1.'),sg.InputText('',key = 'line1'),sg.Checkbox('',key='c1',visible = False)],
    [sg.Text('2.', visible = False),sg.InputText('',key = 'line2', visible = False),sg.Checkbox('',key='c2',visible = False)],
    [sg.Text('3.', visible = False),sg.InputText('',key = 'line3', visible = False),sg.Checkbox('',key='c3',visible = False)],
    [sg.Text('4.', visible = False),sg.InputText('',key = 'line4', visible = False),sg.Checkbox('',key='c4',visible = False)],
    [sg.Text('5.', visible = False),sg.InputText('',key = 'line5', visible = False),sg.Checkbox('',key='c5',visible = False)],
    [sg.Button('+ Add task')],
    [sg.Button('Save'), sg.Button('Exit')]
]

prac2 = [
    [sg.Text('world')],
    [sg.Submit()]]

layout =  [
        [sg.Column(prac1),
        sg.VSeparator(),
        sg.Column(prac2)]]

window = sg.Window('To-Do-List',layout)
while True:
    event, values = window.read()
    if event == sg.WIN_CLOSED or event == 'Exit':
        break
    elif event == '+ Add task':
        """How can i use sg.window[].update here to make lines apear sequentially?"""```

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情徒 2025-01-28 16:36:38

要将全行可见设置或不可见,请更好地使用该行中的所有布局(例如列元素),例如列元素。只需设置指定列元素的可见可见的选项即可设置它。

import datetime
import PySimpleGUI as sg

#Formatting date and time
x = datetime.datetime.now()
y = x.strftime("%x")
z = x.strftime("%A")

def column_layout(index):
    return [[sg.Text(f'{index}.'), sg.InputText('', key=f'line {index}'), sg.Checkbox('', key=f'c {index}')]]

prac1 = [
    [sg.Text(f"Welcome to the To-Do-List for {z}, {y}")]] + [
    [sg.Column(column_layout(index), visible=index==1, key=f'Column {index}')]
        for index in range(1, 6)] + [
    [sg.Button('+ Add task')],
    [sg.Button('Save'), sg.Button('Exit')]]

prac2 = [
    [sg.Text('world')],
    [sg.Submit()]]

layout =  [
    [sg.Column(prac1),
     sg.VSeparator(),
    sg.Column(prac2)]]

window = sg.Window('To-Do-List', layout)
index = 1

while True:

    event, values = window.read()

    if event in (sg.WIN_CLOSED, 'Exit'):
        break

    elif event == '+ Add task':
        index += 1
        if index < 6:
            window[f'Column {index}'].update(visible=True)

window.close()

另一种方法是调用窗口的方法extend_layout,将新行添加到本窗口内部的现有容器元素中。

import datetime
import PySimpleGUI as sg

#Formatting date and time
x = datetime.datetime.now()
y = x.strftime("%x")
z = x.strftime("%A")

def column_layout(index):
    return [[sg.Text(f'{index}.'), sg.InputText('', key=f'line {index}'), sg.Checkbox('', key=f'c {index}')]]

prac1 = [
    [sg.Text(f"Welcome to the To-Do-List for {z}, {y}")]] + [
    [sg.Column(column_layout(1), key='Column')],
    [sg.Button('+ Add task')],
    [sg.Button('Save'), sg.Button('Exit')]]

prac2 = [
    [sg.Text('world')],
    [sg.Submit()]]

layout =  [
    [sg.Column(prac1),
     sg.VSeparator(),
    sg.Column(prac2)]]

window = sg.Window('To-Do-List', layout)
index = 1

while True:

    event, values = window.read()

    if event in (sg.WIN_CLOSED, 'Exit'):
        break

    elif event == '+ Add task':
        index += 1
        if index < 6:
            window.extend_layout(window['Column'], column_layout(index))

window.close()

To set full row visible or not, better use a container, like Column element, for all the layout in that row. Just set option visible of specified Column element to set it visible or not.

import datetime
import PySimpleGUI as sg

#Formatting date and time
x = datetime.datetime.now()
y = x.strftime("%x")
z = x.strftime("%A")

def column_layout(index):
    return [[sg.Text(f'{index}.'), sg.InputText('', key=f'line {index}'), sg.Checkbox('', key=f'c {index}')]]

prac1 = [
    [sg.Text(f"Welcome to the To-Do-List for {z}, {y}")]] + [
    [sg.Column(column_layout(index), visible=index==1, key=f'Column {index}')]
        for index in range(1, 6)] + [
    [sg.Button('+ Add task')],
    [sg.Button('Save'), sg.Button('Exit')]]

prac2 = [
    [sg.Text('world')],
    [sg.Submit()]]

layout =  [
    [sg.Column(prac1),
     sg.VSeparator(),
    sg.Column(prac2)]]

window = sg.Window('To-Do-List', layout)
index = 1

while True:

    event, values = window.read()

    if event in (sg.WIN_CLOSED, 'Exit'):
        break

    elif event == '+ Add task':
        index += 1
        if index < 6:
            window[f'Column {index}'].update(visible=True)

window.close()

another way is to call method extend_layout of window to add new rows to an existing container element inside of this window.

import datetime
import PySimpleGUI as sg

#Formatting date and time
x = datetime.datetime.now()
y = x.strftime("%x")
z = x.strftime("%A")

def column_layout(index):
    return [[sg.Text(f'{index}.'), sg.InputText('', key=f'line {index}'), sg.Checkbox('', key=f'c {index}')]]

prac1 = [
    [sg.Text(f"Welcome to the To-Do-List for {z}, {y}")]] + [
    [sg.Column(column_layout(1), key='Column')],
    [sg.Button('+ Add task')],
    [sg.Button('Save'), sg.Button('Exit')]]

prac2 = [
    [sg.Text('world')],
    [sg.Submit()]]

layout =  [
    [sg.Column(prac1),
     sg.VSeparator(),
    sg.Column(prac2)]]

window = sg.Window('To-Do-List', layout)
index = 1

while True:

    event, values = window.read()

    if event in (sg.WIN_CLOSED, 'Exit'):
        break

    elif event == '+ Add task':
        index += 1
        if index < 6:
            window.extend_layout(window['Column'], column_layout(index))

window.close()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文