Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Json Python3 tkinter标签值_Json_Python 3.x_Tkinter - Fatal编程技术网

Json Python3 tkinter标签值

Json Python3 tkinter标签值,json,python-3.x,tkinter,Json,Python 3.x,Tkinter,我需要一些帮助,我尝试在按下按钮后更新etcprice标签值,每5秒更新一次,在terminal works中,但在tk窗口中不更新。我在这里绊倒了:(请帮帮我 import urllib.request from urllib.request import * import json import six from tkinter import * import tkinter as tk import threading p

我需要一些帮助,我尝试在按下按钮后更新etcprice标签值,每5秒更新一次,在terminal works中,但在tk窗口中不更新。我在这里绊倒了:(请帮帮我

    import urllib.request
    from urllib.request import *
    import json
    import six
    from tkinter import *
    import tkinter as tk
    import threading


    price = '0'

    def timer():
        threading.Timer(5.0, timer).start()
        currentPrice()

    def currentPrice():
        url = 'https://api.cryptowat.ch/markets/bitfinex/ethusd/price'
        json_obj = urllib.request.urlopen(url)
        data = json.load(json_obj)
        for item, v in six.iteritems(data['result']):
            # print("ETC: $", v)
            price = str(v)
          # print(type(etcar))
            print(price)
        return price

    def windows():
        root = Tk()
        root.geometry("500x200")

        kryptoname = Label(root, text="ETC price: ")
        kryptoname.grid(column=0, row=0)

        etcprice = Label(root, textvariable=price)
        etcprice.grid(column=1, row=0)

        updatebtn = Button(root, text="update", command=timer)
        updatebtn.grid(column=0, row=1)
        root.mainloop()

    windows()
我试图将“price”设置为“StringVar()”,但在这种情况下,我得到了很多错误

    import urllib.request
    from urllib.request import *
    import json
    import six
    from tkinter import *
    import tkinter as tk
    import threading


    price = '0'

    def timer():
        threading.Timer(5.0, timer).start()
        currentPrice()

    def currentPrice():
        url = 'https://api.cryptowat.ch/markets/bitfinex/ethusd/price'
        json_obj = urllib.request.urlopen(url)
        data = json.load(json_obj)
        for item, v in six.iteritems(data['result']):
            # print("ETC: $", v)
            price = str(v)
          # print(type(etcar))
            print(price)
        return price

    def windows():
        root = Tk()
        root.geometry("500x200")

        kryptoname = Label(root, text="ETC price: ")
        kryptoname.grid(column=0, row=0)

        etcprice = Label(root, textvariable=price)
        etcprice.grid(column=1, row=0)

        updatebtn = Button(root, text="update", command=timer)
        updatebtn.grid(column=0, row=1)
        root.mainloop()

    windows()
非常感谢

    import urllib.request
    from urllib.request import *
    import json
    import six
    from tkinter import *
    import tkinter as tk
    import threading


    price = '0'

    def timer():
        threading.Timer(5.0, timer).start()
        currentPrice()

    def currentPrice():
        url = 'https://api.cryptowat.ch/markets/bitfinex/ethusd/price'
        json_obj = urllib.request.urlopen(url)
        data = json.load(json_obj)
        for item, v in six.iteritems(data['result']):
            # print("ETC: $", v)
            price = str(v)
          # print(type(etcar))
            print(price)
        return price

    def windows():
        root = Tk()
        root.geometry("500x200")

        kryptoname = Label(root, text="ETC price: ")
        kryptoname.grid(column=0, row=0)

        etcprice = Label(root, textvariable=price)
        etcprice.grid(column=1, row=0)

        updatebtn = Button(root, text="update", command=timer)
        updatebtn.grid(column=0, row=1)
        root.mainloop()

    windows()
解决方案是:我创建了一个名为“b”的新字符串变量,并将etcprice标签变量更改为此。
在我将这个b.set(price)添加到currentPrice()def:中之后,它就开始工作了。

这个
price
变量是一个全局变量-如果您试图更改它,您需要显式地这样做:

    import urllib.request
    from urllib.request import *
    import json
    import six
    from tkinter import *
    import tkinter as tk
    import threading


    price = '0'

    def timer():
        threading.Timer(5.0, timer).start()
        currentPrice()

    def currentPrice():
        url = 'https://api.cryptowat.ch/markets/bitfinex/ethusd/price'
        json_obj = urllib.request.urlopen(url)
        data = json.load(json_obj)
        for item, v in six.iteritems(data['result']):
            # print("ETC: $", v)
            price = str(v)
          # print(type(etcar))
            print(price)
        return price

    def windows():
        root = Tk()
        root.geometry("500x200")

        kryptoname = Label(root, text="ETC price: ")
        kryptoname.grid(column=0, row=0)

        etcprice = Label(root, textvariable=price)
        etcprice.grid(column=1, row=0)

        updatebtn = Button(root, text="update", command=timer)
        updatebtn.grid(column=0, row=1)
        root.mainloop()

    windows()
def currentPrice():
    global price
    url = 'https://api.cryptowat.ch/markets/bitfinex/ethusd/price'
    json_obj = urllib.request.urlopen(url)
    data = json.load(json_obj)
    for item, v in six.iteritems(data['result']):
        # print("ETC: $", v)
        price = str(v)
      # print(type(etcar))
        print(price)
    return price
否则,Python会将其“镜像”为函数内部的局部变量,而不会修改全局变量

    import urllib.request
    from urllib.request import *
    import json
    import six
    from tkinter import *
    import tkinter as tk
    import threading


    price = '0'

    def timer():
        threading.Timer(5.0, timer).start()
        currentPrice()

    def currentPrice():
        url = 'https://api.cryptowat.ch/markets/bitfinex/ethusd/price'
        json_obj = urllib.request.urlopen(url)
        data = json.load(json_obj)
        for item, v in six.iteritems(data['result']):
            # print("ETC: $", v)
            price = str(v)
          # print(type(etcar))
            print(price)
        return price

    def windows():
        root = Tk()
        root.geometry("500x200")

        kryptoname = Label(root, text="ETC price: ")
        kryptoname.grid(column=0, row=0)

        etcprice = Label(root, textvariable=price)
        etcprice.grid(column=1, row=0)

        updatebtn = Button(root, text="update", command=timer)
        updatebtn.grid(column=0, row=1)
        root.mainloop()

    windows()
每次单击该按钮时,继续启动越来越多的线程不是一个好主意-因此:

    import urllib.request
    from urllib.request import *
    import json
    import six
    from tkinter import *
    import tkinter as tk
    import threading


    price = '0'

    def timer():
        threading.Timer(5.0, timer).start()
        currentPrice()

    def currentPrice():
        url = 'https://api.cryptowat.ch/markets/bitfinex/ethusd/price'
        json_obj = urllib.request.urlopen(url)
        data = json.load(json_obj)
        for item, v in six.iteritems(data['result']):
            # print("ETC: $", v)
            price = str(v)
          # print(type(etcar))
            print(price)
        return price

    def windows():
        root = Tk()
        root.geometry("500x200")

        kryptoname = Label(root, text="ETC price: ")
        kryptoname.grid(column=0, row=0)

        etcprice = Label(root, textvariable=price)
        etcprice.grid(column=1, row=0)

        updatebtn = Button(root, text="update", command=timer)
        updatebtn.grid(column=0, row=1)
        root.mainloop()

    windows()
updatebtn = Button(root, text="update", command=currentPrice)
可能更有意义

    import urllib.request
    from urllib.request import *
    import json
    import six
    from tkinter import *
    import tkinter as tk
    import threading


    price = '0'

    def timer():
        threading.Timer(5.0, timer).start()
        currentPrice()

    def currentPrice():
        url = 'https://api.cryptowat.ch/markets/bitfinex/ethusd/price'
        json_obj = urllib.request.urlopen(url)
        data = json.load(json_obj)
        for item, v in six.iteritems(data['result']):
            # print("ETC: $", v)
            price = str(v)
          # print(type(etcar))
            print(price)
        return price

    def windows():
        root = Tk()
        root.geometry("500x200")

        kryptoname = Label(root, text="ETC price: ")
        kryptoname.grid(column=0, row=0)

        etcprice = Label(root, textvariable=price)
        etcprice.grid(column=1, row=0)

        updatebtn = Button(root, text="update", command=timer)
        updatebtn.grid(column=0, row=1)
        root.mainloop()

    windows()
您不需要在这里使用线程,只需“在后台”调用函数。您可以使用tkinter自己的.after函数来延迟校准函数。(顺便说一句,它使用毫秒,而不是浮点秒值)

    import urllib.request
    from urllib.request import *
    import json
    import six
    from tkinter import *
    import tkinter as tk
    import threading


    price = '0'

    def timer():
        threading.Timer(5.0, timer).start()
        currentPrice()

    def currentPrice():
        url = 'https://api.cryptowat.ch/markets/bitfinex/ethusd/price'
        json_obj = urllib.request.urlopen(url)
        data = json.load(json_obj)
        for item, v in six.iteritems(data['result']):
            # print("ETC: $", v)
            price = str(v)
          # print(type(etcar))
            print(price)
        return price

    def windows():
        root = Tk()
        root.geometry("500x200")

        kryptoname = Label(root, text="ETC price: ")
        kryptoname.grid(column=0, row=0)

        etcprice = Label(root, textvariable=price)
        etcprice.grid(column=1, row=0)

        updatebtn = Button(root, text="update", command=timer)
        updatebtn.grid(column=0, row=1)
        root.mainloop()

    windows()
可能是一种有用的功能

    import urllib.request
    from urllib.request import *
    import json
    import six
    from tkinter import *
    import tkinter as tk
    import threading


    price = '0'

    def timer():
        threading.Timer(5.0, timer).start()
        currentPrice()

    def currentPrice():
        url = 'https://api.cryptowat.ch/markets/bitfinex/ethusd/price'
        json_obj = urllib.request.urlopen(url)
        data = json.load(json_obj)
        for item, v in six.iteritems(data['result']):
            # print("ETC: $", v)
            price = str(v)
          # print(type(etcar))
            print(price)
        return price

    def windows():
        root = Tk()
        root.geometry("500x200")

        kryptoname = Label(root, text="ETC price: ")
        kryptoname.grid(column=0, row=0)

        etcprice = Label(root, textvariable=price)
        etcprice.grid(column=1, row=0)

        updatebtn = Button(root, text="update", command=timer)
        updatebtn.grid(column=0, row=1)
        root.mainloop()

    windows()
然后,在启动mainloop之前,或者当您想要启动时,请调用它一次:

    import urllib.request
    from urllib.request import *
    import json
    import six
    from tkinter import *
    import tkinter as tk
    import threading


    price = '0'

    def timer():
        threading.Timer(5.0, timer).start()
        currentPrice()

    def currentPrice():
        url = 'https://api.cryptowat.ch/markets/bitfinex/ethusd/price'
        json_obj = urllib.request.urlopen(url)
        data = json.load(json_obj)
        for item, v in six.iteritems(data['result']):
            # print("ETC: $", v)
            price = str(v)
          # print(type(etcar))
            print(price)
        return price

    def windows():
        root = Tk()
        root.geometry("500x200")

        kryptoname = Label(root, text="ETC price: ")
        kryptoname.grid(column=0, row=0)

        etcprice = Label(root, textvariable=price)
        etcprice.grid(column=1, row=0)

        updatebtn = Button(root, text="update", command=timer)
        updatebtn.grid(column=0, row=1)
        root.mainloop()

    windows()
timer(5000, root, currentPrice)
例如,如果您希望currentPrice函数在单独的线程中运行,从而在出现网络延迟时不会阻塞主GUI线程,那么您可以使用如下线程:

    import urllib.request
    from urllib.request import *
    import json
    import six
    from tkinter import *
    import tkinter as tk
    import threading


    price = '0'

    def timer():
        threading.Timer(5.0, timer).start()
        currentPrice()

    def currentPrice():
        url = 'https://api.cryptowat.ch/markets/bitfinex/ethusd/price'
        json_obj = urllib.request.urlopen(url)
        data = json.load(json_obj)
        for item, v in six.iteritems(data['result']):
            # print("ETC: $", v)
            price = str(v)
          # print(type(etcar))
            print(price)
        return price

    def windows():
        root = Tk()
        root.geometry("500x200")

        kryptoname = Label(root, text="ETC price: ")
        kryptoname.grid(column=0, row=0)

        etcprice = Label(root, textvariable=price)
        etcprice.grid(column=1, row=0)

        updatebtn = Button(root, text="update", command=timer)
        updatebtn.grid(column=0, row=1)
        root.mainloop()

    windows()
Thread(target=currentPrice, daemon=True).start()
它将在守护进程线程中运行-如果关闭程序或ctrl-c或其他任何操作,它将自动被终止。因此,您可以将该行放入
getCurrentPriceBG
或类似函数中

    import urllib.request
    from urllib.request import *
    import json
    import six
    from tkinter import *
    import tkinter as tk
    import threading


    price = '0'

    def timer():
        threading.Timer(5.0, timer).start()
        currentPrice()

    def currentPrice():
        url = 'https://api.cryptowat.ch/markets/bitfinex/ethusd/price'
        json_obj = urllib.request.urlopen(url)
        data = json.load(json_obj)
        for item, v in six.iteritems(data['result']):
            # print("ETC: $", v)
            price = str(v)
          # print(type(etcar))
            print(price)
        return price

    def windows():
        root = Tk()
        root.geometry("500x200")

        kryptoname = Label(root, text="ETC price: ")
        kryptoname.grid(column=0, row=0)

        etcprice = Label(root, textvariable=price)
        etcprice.grid(column=1, row=0)

        updatebtn = Button(root, text="update", command=timer)
        updatebtn.grid(column=0, row=1)
        root.mainloop()

    windows()