小编Sam*_*eld的帖子

在Tkinter中显示matplotlib条形图

我正在尝试在Tkinter窗口中显示matplotlib条形图。我发现了很多有关如何放置折线图的教程,例如:http : //matplotlib.org/examples/user_interfaces/embedding_in_tk.html

但是我找不到一个可放入条形图的图表。我知道制作条形图的唯一方法是这样的:http : //matplotlib.org/examples/api/barchart_demo.html。显然,条形图示例中导入的模块与Tkinter示例中导入的模块不同,如果可以的话,我不确定如何使其工作。

长话短说,有人可以向我提供在Tkinter窗口中显示的matplotlib条形图示例吗?谢谢。

python charts tkinter matplotlib

3
推荐指数
1
解决办法
5781
查看次数

使用 Python 在 Tkinter 中保存异常

我为使用 Tkinter 接收用户输入的其他人开发了几个 Python 程序。为了保持简单和用户友好,命令行或 python 控制台从不出现(即使用 .pyw 文件),所以我正在研究使用日志库将错误文本写入文件时发生异常。但是,我很难让它真正捕获异常。例如:

我们编写了一个会导致错误的函数:

def cause_an_error():
    a = 3/0
Run Code Online (Sandbox Code Playgroud)

现在我们尝试正常记录错误:

import logging
logging.basicConfig(filename='errors.log', level=logging.ERROR)

try:
    cause_an_error()
except:
    logging.exception('simple-exception')
Run Code Online (Sandbox Code Playgroud)

正如预期的那样,程序出错,日志记录将错误写入 errors.log。控制台中什么也没有出现。但是,当我们实现 Tkinter 接口时,会有不同的结果,如下所示:

import logging
import Tkinter
logging.basicConfig(filename='errors.log', level=logging.ERROR)

try:
    root = Tkinter.Tk()
    Tkinter.Button(root, text='Test', command=cause_an_error).pack()
    root.mainloop()
except:
    logging.exception('simple-exception')
Run Code Online (Sandbox Code Playgroud)

在这种情况下,按下 Tkinter 窗口中的按钮会导致错误。但是,这一次,没有写入文件,控制台中出现以下错误:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Python27\lib\lib-tk\Tkinter.py", line 1536, in __call__
    return self.func(*args)
  File "C:/Users/samk/Documents/GitHub/sandbox/sandbox2.pyw", line 8, in cause_an_error
    a = 3/0
ZeroDivisionError: integer division or modulo by zero …
Run Code Online (Sandbox Code Playgroud)

python tkinter

3
推荐指数
1
解决办法
1250
查看次数

标签 统计

python ×2

tkinter ×2

charts ×1

matplotlib ×1