Tkinter中的ASCII艺术在控制台中看起来不像

时间:2019-09-23 16:26:48

标签: python tkinter format ascii-art

我有以下Python(3.7.2)代码:

 import tkinter as tk
 root = tk.Tk()

 art = '''

                 uu$$$$$$$$$$$uu
              uu$$$$$$$$$$$$$$$$$uu
             u$$$$$$$$$$$$$$$$$$$$$u
            u$$$$$$$$$$$$$$$$$$$$$$$u
           u$$$$$$$$$$$$$$$$$$$$$$$$$u
           u$$$$$$*   *$$$*   *$$$$$$u
           *$$$$*      u$u       $$$$*
            $$$u       u$u       u$$$
            $$$u      u$$$u      u$$$
             *$$$$uu$$$   $$$uu$$$$*
              *$$$$$$$*   *$$$$$$$*
                u$$$$$$$u$$$$$$$u
                 u$*$*$*$*$*$*$u
      uuu        $$u$ $ $ $ $u$$       uuu
      u$$$$       $$$$$u$u$u$$$       u$$$$
      $$$$$uu      *$$$$$$$$$*     uu$$$$$$
    u$$$$$$$$$$$uu    *****    uuuu$$$$$$$$$
    $$$$***$$$$$$$$$$uuu   uu$$$$$$$$$***$$$*
     ***      **$$$$$$$$$$$uu **$***
              uuuu **$$$$$$$$$$uuu
     u$$$uuu$$$$$$$$$uu **$$$$$$$$$$$uuu$$$
     $$$$$$$$$$****           **$$$$$$$$$$$*
       *$$$$$*                      **$$$$**
         $$$*                         $$$$*

'''

print(art)

label = tk.Label(text=art)
label.pack()

root.mainloop()

它将在控制台和Tkinter中打印头骨。但是在Tkinter中,其格式错误。 (不会引发任何错误)

enter image description here

问题

  1. 为什么Tkinter(标签)有另一种格式?
  2. 我该如何解决?

This是唯一相关的帖子,但没有帮助。

1 个答案:

答案 0 :(得分:4)

您需要使用等宽字体来使ASCII艺术作品正常工作。使用此示例:

import Tkinter
import ttk

style = ttk.Style()
style.configure('Style.TButton', font='TkFixedFont')

label = tk.Label(text=art, style='Style.TButton')