简体   繁体   English

在 Python3 中发出 HTTP 请求时的 GTK3 Spinner

[英]GTK3 Spinner while making HTTP requests in Python3

I seen a lot, on the Internet and eventhis official tutorial hasn't solved my issue.我在网上看到了很多,甚至这个官方教程也没有解决我的问题。

I need a way to make HTTP requests, while Gtk.Spinner spins.我需要一种方法来发出 HTTP 请求,同时 Gtk.Spinner 旋转。 I have so far this:到目前为止,我有这个:

My class:我的课:

class SpinnerBox(Gtk.Window):
    def __init__(self):
         super().__init__()
         self.spinner = Gtk.Spinner()
         self.button = Gtk.Button()
         self.box = Gtk.Box()
         self.box.pack_start(self.spin, True, True, 0)
         self.box.pack_start(self.button, True, True, 0)
         self.button.connect("clicked", self.on_button_click) # when user clicks the button
         self.add(self.box)

def query(caller, url):
     requests.post(url, '007') # This part of code takes 10 sec
     GLib.idle_add(caller.spin_it)

    def on_button_click(self): # this runs on button click
        session = threading.Thread(target=query, args=(self, "http://example.com/validate"))
        session.daemon = True
        session.start()

    def spin_it(self):
        self.spinner.start()

But this solution DOESN'T WORK!但是这个解决方案不起作用! It does requests first (without spinning), then AFTER REQUESTS ARE DONE it actually starts spinning.它首先请求(不旋转),然后在请求完成后它实际上开始旋转。 How can I make it spinning, while doing requests (can be more than one) ?我怎样才能让它旋转,同时做请求(可以不止一个)?

UPD: Okay, seems that I should run spinner first, then create Thread, ie good solution is: UPD:好的,看来我应该先运行微调器,然后创建线程,即好的解决方案是:

def on_button_click(self): # this runs on button click
    self.spinner.start()
    session = threading.Thread(target=query, args=(self, "http://example.com/validate"))
    session.daemon = True
    session.start()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM