Tkinter textvariable未使用MatplotLib更新 [英] Tkinter textvariable not updated with MatplotLib

查看:173
本文介绍了Tkinter textvariable未使用MatplotLib更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以在这里帮助我吗?当我添加以下行时,textvariable不会被更新:

Can anyone help me here? The textvariable is not being updated when I add the line:

f = plt.figure(0, figsize=(20,9))

如果我在上面的行中添加了注释,那么我会看到Label中的textvariable正在更新,但是取消注释该行后,Label便不再更新. 有人可以帮我吗?

If I comment the line above, then I see the textvariable being updated in the Label, but as soon as I uncomment the line, the Label is not updated anymore. Can anyone help me, please?

from tkinter import *
from tkinter import ttk
from tkinter import StringVar

import matplotlib
import matplotlib.artist as artists
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2Tk
from matplotlib.figure import Figure
import matplotlib.animation as animation

f = plt.figure(0, figsize=(20,9))

class make_window():

    def __init__(self, *args, **kwargs):

        self.win = Tk()
        self.win.title("Test")
        self.win.state("zoomed")

        Frame = ttk.Frame(self.win)
        Frame.pack()

        labelVar = StringVar()
        labelVar.set("Hi")
        self.LabelUpdate = Label(Frame, textvariable = labelVar)
        self.LabelUpdate.pack()


#################################################################################

window = make_window()
window.win.mainloop()

推荐答案

我注意到您需要解决一些问题.

I have noticed a few issues that you need to address.

首先,导入方式(from tkinter import *)会引起问题,实际上,在这种情况下,您会犯这种导入众所周知的错误.稍后在代码中执行Frame = ttk.Frame(),而这一行实际上是覆盖从from tkinter import *导入的Frame.不要将变量的名称与内置方法的名称相同.为了防止这种错误,您可以简单地执行import tkinter as tk并始终使用tk.前缀来防止代码中的任何覆盖.

First the way you are importing (from tkinter import *) is going to cause problems and in fact here in this case you make the kind of mistake this import is known for. Later in your code you do Frame = ttk.Frame() and this one line is actually overriding the Frame import from from tkinter import *. Do not name your variables the same as built in methods. In order to prevent this kind of mistake you can simply do import tkinter as tk and always use the tk. prefix to prevent any overriding in your code.

接下来,我注意到您的标签确实装在屏幕上,但没有显示任何奇怪的文字,但是当我注释掉f = plt.figure(0, figsize=(20,9))时,标签开始按预期工作.

Next I noticed your Label does pack to the screen but does not show any text oddly enough but when I comment out f = plt.figure(0, figsize=(20,9)) the Label starts to work as expected.

因此,与matplotlib和此标签存在一些奇怪的相互作用.

So there is some odd interaction here with matplotlib and this label.

当我将f = plt.figure(0, figsize=(20,9))更改为f = Figure(figsize=(20,9))时,标签也会再次起作用.

When I change f = plt.figure(0, figsize=(20,9)) to f = Figure(figsize=(20,9)) the label also works again.

如果Figure()并不完全是您要在此处使用的内容,那么请在代码中添加更多内容,以了解您希望使用f做些什么.

If Figure() is not exactly what you are trying to use here then please add some more context to your code as to what you are expecting to do with f.

更新:

我也确实注意到,当您将f移到self.win = tk.Tk()之后时,代码可以正常工作.我唯一的猜测是为什么会发生这种情况,可能是matplotlib确实有自己的tkinter代码.因此,如果您尝试在创建tkinter实例之前创建plt.figure,那么我在后端成像了matplotlib正在运行tkinter实例,这就是导致异常行为的原因.我可能是错的,但我别无选择.也许Tkinter大师布莱恩·奥克利(Bryan Oakley)可以启发我们有关造成此问题的原因:D

I did also notice that when you move f to be after self.win = tk.Tk() the code works fine. My only guess as to why this is happening is possible how matplotlib does have its own tkinter code in it. So if you try to create a plt.figure before you create your tkinter instance then I imaging in the back end a tkinter instance is being ran by matplotlib and this is what is causing the odd behavior. I could be wrong but I cannot think of anything else. Maybe the Tkinter master Bryan Oakley can enlighten us as to what is causing this :D

import tkinter as tk
import tkinter.ttk as ttk

import matplotlib
import matplotlib.artist as artists
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2Tk
from matplotlib.figure import Figure
import matplotlib.animation as animation


class make_window():
    def __init__(self, *args, **kwargs):
        self.win = tk.Tk()
        # moving the line to here should at least allow your code to work.
        f = plt.figure(0, figsize=(20,9))
        self.win.title("Test")
        self.win.state("zoomed")

        frame = ttk.Frame(self.win)
        frame.pack()

        labelVar = tk.StringVar()
        labelVar.set("Hi")
        self.LabelUpdate = tk.Label(frame, textvariable = labelVar)
        self.LabelUpdate.pack()


window = make_window()
window.win.mainloop()

这篇关于Tkinter textvariable未使用MatplotLib更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆