【问题标题】:python3 tkinter and matplot conflictpython3 tkinter和matplot冲突
【发布时间】:2020-07-23 09:48:33
【问题描述】:

我正在尝试用 tkinter 制作一个浮点滑块。导入 matplotlib.py 后,滑块停止工作。有人知道为什么会这样吗?谢谢您的帮助。 PS我是新手,所以我不知道如何格式化下面的代码,很抱歉。

# !/usr/bin/python3
from tkinter import *
#from functools import partial
#import pygame as pg
#import numpy as np
#import sympy as sym
#import mpmath
import matplotlib.pyplot as plt
from math import *
#from pn import *
from gui import *

Panel_bczony_width=205

_width=1200
_height=800
zakres_gora      =[50 ,30,200,7,1000,1000,360,100]
zakres_dol       =[1  ,1,1  ,3,   1,1   ,330,  1]

class Parametry_ukladu:
    def __init__(self, master):
        self.master=master
        self.pom=Label(self.master, text="Parametry Układu", width=25)
        self.pom.pack()
        self.scale=[]
        self.frame=[]
        self.label=[]
        self.pom=0
        for x in range(8):
            self.frame.append(Frame(self.master))
            self.label.append(Label( self.frame[x],  width=10))           
            #self.scale.append(Scale( self.frame[x], from_=zakres_dol[x], to=zakres_gora[x], orient=HORIZONTAL, command=lambda x: set_zmienne(self.scale, x)))
            self.scale.append(Scale( self.frame[x], from_=1.0, to=100.0, orient=HORIZONTAL, resolution=0.1))

            #self.scale[x].set(default_values[x])        
            self.label[x].pack(side=LEFT)            
            self.scale[x].pack(side=RIGHT)
            self.frame[x].pack(side=TOP)
#wczytaj("domyslne_dane.txt")
root = Tk()
parametry_u = Parametry_ukladu(root)
root.mainloop()

编辑。我在这里发现:Tkinter Matplotlib, backend conflict? 更改 matplotlib 的后端应该可以解决问题。为此,我称之为某事。像这样:

import matplotlib
matplotlib.use('WxAgg')
import matplotlib.pyplot as plt

但是当我尝试使用例如 plt.plot() 我得到这个错误:

Traceback (most recent call last):
  File "main.py", line 17, in <module>
    import matplotlib.pyplot as plt
  File "/home/plebania/.local/lib/python3.6/site-packages/matplotlib/pyplot.py", line 2355, in <module>
    switch_backend(rcParams["backend"])
  File "/home/plebania/.local/lib/python3.6/site-packages/matplotlib/pyplot.py", line 221, in switch_backend
    backend_mod = importlib.import_module(backend_name)
  File "/usr/lib/python3.6/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "/home/plebania/.local/lib/python3.6/site-packages/matplotlib/backends/backend_wxagg.py", line 1, in <module>
    import wx
ModuleNotFoundError: No module named 'wx'

【问题讨论】:

  • 你应该避免导入*,原因如下:stackoverflow.com/questions/2386714/why-is-import-bad
  • 我已将 tkinter import 更改为 import tkinter as tk 但行为没有改变
  • 如果你在你的程序中使用 Tkinter,你可能想使用 matplotlib 的 Tkinter 后端 - TkAgg,我想。
  • jasonharper 谢谢这是一个案例:)

标签: python tkinter


【解决方案1】:

请在调试之前尝试替换所有 * 导入。您可以逐一尝试,直到找到导致问题的确切原因。 “停止工作”是什么意思?滑块是否仍然显示?你能用鼠标/键盘移动它们吗?他们会响应其他事件吗?

【讨论】:

  • 我已经完成了。我评论了所有导入,但 matplotlib.pyplot,并将 tkinter 导入更改为将 tkinter 导入为 tk。没有结果。添加 matplotlib.use('WxAgg') 超级解决了问题,但后来我不知道如何使用 matplotlib 绘图...
猜你喜欢
  • 1970-01-01
  • 2012-04-05
  • 1970-01-01
  • 2020-04-30
  • 1970-01-01
  • 2020-07-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多