Tkinter блокирует Python, когда значок загружается, и tk.mainloop находится в потоке

Установите 'http_only' опцию в хеше, используемом для установки cookie

, например,

cookies["user_name"] = { :value => "david", :httponly => true }

или в направляющих 2:

, например,

cookies["user_name"] = { :value => "david", :http_only => true }
7
задан Peter Mortensen 14 October 2017 в 12:25
поделиться

1 ответ

Я считаю, что вам не следует выполнять основной цикл в другом потоке. AFAIK, основной цикл должен выполняться в том же потоке, который создал виджет.

Наборы инструментов графического интерфейса, с которыми я знаком (Tkinter, .NET Windows Forms), таковы: вы можете управлять графическим интерфейсом только из одного потока.

В Linux ваш код вызывает исключение:

self.tk.mainloop(n)
RuntimeError: Calling Tcl from different appartment

Одно из следующее будет работать (без дополнительных потоков):

if __name__ == '__main__':
    t = tk.Tk()
    t.iconbitmap('icon.ico')

    b = tk.Button(text='test', command=exit)
    b.grid(row=0)

    t.mainloop()

С дополнительным потоком:

def threadmain():
    t = tk.Tk()
    t.iconbitmap('icon.ico')
    b = tk.Button(text='test', command=exit)
    b.grid(row=0)
    t.mainloop()


if __name__ == '__main__':
    thread.start_new_thread(threadmain, ())

    while 1:
        sleep(1)

Если вам нужно взаимодействовать с tkinter извне потока tkinter, я предлагаю вам установить таймер, который проверяет очередь на работу.

] Вот пример:

import Tkinter as tk
import thread
from time import sleep
import Queue

request_queue = Queue.Queue()
result_queue = Queue.Queue()

def submit_to_tkinter(callable, *args, **kwargs):
    request_queue.put((callable, args, kwargs))
    return result_queue.get()

t = None
def threadmain():
    global t

    def timertick():
        try:
            callable, args, kwargs = request_queue.get_nowait()
        except Queue.Empty:
            pass
        else:
            print "something in queue"
            retval = callable(*args, **kwargs)
            result_queue.put(retval)

        t.after(500, timertick)

    t = tk.Tk()
    t.configure(width=640, height=480)
    b = tk.Button(text='test', name='button', command=exit)
    b.place(x=0, y=0)
    timertick()
    t.mainloop()

def foo():
    t.title("Hello world")

def bar(button_text):
    t.children["button"].configure(text=button_text)

def get_button_text():
    return t.children["button"]["text"]

if __name__ == '__main__':
    thread.start_new_thread(threadmain, ())

    trigger = 0
    while 1:
        trigger += 1

        if trigger == 3:
            submit_to_tkinter(foo)

        if trigger == 5:
            submit_to_tkinter(bar, "changed")

        if trigger == 7:
            print submit_to_tkinter(get_button_text)

        sleep(1)
23
ответ дан 6 December 2019 в 07:52
поделиться
Другие вопросы по тегам:

Похожие вопросы: