【问题标题】:Passed a parameter and managed to update that inside the function - Python传递一个参数并设法在函数内部更新它 - Python
【发布时间】:2021-07-16 03:52:49
【问题描述】:

我从 PySimpleGUI 看到了一个示例演示代码,但我仍然不确定 window 变量是否如何

窗口作为参数传递给 the_thread() 函数,它设法对主函数内部的 window.write_event_value 等窗口变量执行一些操作。

我在 main 中有一个 cp(values) 证明它是被添加的。

谁能给我解释一下这怎么可能?

import threading
import time
import PySimpleGUI as sg


THREAD_EVENT = '-THREAD-'

cp = sg.cprint

def the_thread(window):

    i = 0
    while True:
        time.sleep(1)
        cp('This keeps on running')
        window.write_event_value('-THREAD-', (threading.current_thread().name, i))      # Data sent is a tuple of thread name and counter
        cp('This is cheating from the thread', c='white on green')
        i += 1


def main():

    layout = [  [sg.Text('Output Area - cprint\'s route to here', font='Any 15')],
                [sg.Multiline(size=(65,20), key='-ML-', autoscroll=True, reroute_stdout=True, write_only=True, reroute_cprint=True)],
                [sg.T('Input so you can see data in your dictionary')],
                [sg.Input(key='-IN-', size=(30,1))],
                [sg.B('Start A Thread'), sg.B('Dummy'), sg.Button('Exit')]  ]

    window = sg.Window('Window Title', layout)

    while True:             # Event Loop
        event, values = window.read()
        
        if event == sg.WIN_CLOSED or event == 'Exit':
            break
        if event.startswith('Start'):
            threading.Thread(target=the_thread, args=(window,), daemon=True).start()
        if event == THREAD_EVENT:
            cp(f'Data from the thread ', colors='white on purple', end='')
            cp(f'{values[THREAD_EVENT]}', colors='white on red')

        cp(values)
    window.close()


if __name__ == '__main__':
    main()

【问题讨论】:

  • 我试图回答你的问题,但不明白这是什么问题?

标签: python function pysimplegui


【解决方案1】:

对不起,我是菜鸟,看起来它在单独的函数上被修改的原因是因为它是一个可变对象。

【讨论】:

    【解决方案2】:

    仍然不确定问题是什么。 试着解释一下这个脚本。

    1. 定义窗口布局
        layout = [  [sg.Text('Output Area - cprint\'s route to here', font='Any 15')],
                    [sg.Multiline(size=(65,20), key='-ML-', autoscroll=True, reroute_stdout=True, write_only=True, reroute_cprint=True)],
                    [sg.T('Input so you can see data in your dictionary')],
                    [sg.Input(key='-IN-', size=(30,1))],
                    [sg.B('Start A Thread'), sg.B('Dummy'), sg.Button('Exit')]  ]
    
    1. 定义窗口
    window = sg.Window('Window Title', layout)
    
    1. While 循环处理由鼠标或键盘输入操作在窗口上产生的事件。这里window.read()会在更新前创建窗口的GUI,如果GUI还没有最终确定,然后更新窗口的GUI
        while True:             # Event Loop
            event, values = window.read()
        ...       
    
    1. 如果单击窗口关闭按钮的事件或单击“退出”按钮的事件,则中断 while 循环。
            if event == sg.WIN_CLOSED or event == 'Exit':
                break
    
    1. 如果单击“启动线程”按钮,则通过调用函数the_thread(window) 启动新线程。
            if event.startswith('Start'):
                threading.Thread(target=the_thread, args=(window,), daemon=True).start()
    
    1. 线程函数每休眠一秒发送一个事件'-THREAD-',值为(threading.current_thread().name, i)。一般来说,你不应该在主线程中更新窗口的 GUI,因为你的代码运行时可能会遇到麻烦,向主线程发送一个事件来处理 GUI 更新,所以我在这里删除了所有 cp 调用。
    def the_thread(window):
    
        i = 0
        while True:
            time.sleep(1)
            window.write_event_value('-THREAD-', (threading.current_thread().name, i))      # Data sent is a tuple of thread name and counter
            i += 1
    
    1. 更新元素或窗口,并在线程产生事件-THREAD-时向sg.Multiline打印消息。
            if event == THREAD_EVENT:
                cp(f'Data from the thread ', colors='white on purple', end='')
                cp(f'{values[THREAD_EVENT]}', colors='white on red')
    
    1. 在 while 循环中断后,关闭 window 并退出。
        window.close()
    

    【讨论】:

    • 对不起,我是 python 的新手,我还在学习,我的问题是为什么窗口的属性在一个单独的函数中更新,以及当它在 main 中时如何从那里访问它。我现在意识到,当你传递一个可变对象时,你可以修改它的属性,无论是列表还是类对象。这是一个愚蠢的问题,很抱歉给您带来麻烦。我将继续学习以了解有关该语言的更多信息。
    猜你喜欢
    • 1970-01-01
    • 2015-10-05
    • 1970-01-01
    • 2021-06-02
    • 2018-09-13
    • 1970-01-01
    • 2021-09-29
    • 2018-09-26
    • 2020-11-05
    相关资源
    最近更新 更多