Python中列表和字典的文本换行

2024-03-28 22:15:23 发布

您现在位置:Python中文网/ 问答频道 /正文

我创建了一个程序,用pythontkinter找出一组数字的所有可能组合。但是当输出发送到GUI时。输出布局非常混乱(见图)

The output of my program

我在output_text.configure中使用了wrap = 195,但是它没有很好地整理输出。另外,我尝试使用warp = "WORD",它发出以下错误:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Python\lib\tkinter\__init__.py", line 1883, in __call__
    return self.func(*args)
  File "C:\Eclipse IDE\Workspace\OCR A-LEVEL Programming Challenges\PIN Code Sequencer.py", line 15, in btn1_clicked
    output_text.configure(text = "Output: " + str(output1), wrap="WORD")
  File "C:\Python\lib\tkinter\__init__.py", line 1637, in configure
    return self._configure('configure', cnf, kw)
  File "C:\Python\lib\tkinter\__init__.py", line 1627, in _configure
    self.tk.call(_flatten((self._w, cmd)) + self._options(cnf))
_tkinter.TclError: bad screen distance "WORD"

我想在一条线上显示2-3个组合

这是我的密码:

from tkinter import *
from itertools import *

window =Tk()
window.geometry("480x270")
window.title("PIN Code Combinations")

title1 = Label(window, text = "Input Numbers To Find Out All the Possible Combination!")
title1.grid(row = 0, column = 0)

input1 = Entry(window, width = 20)
input1.grid(row = 1, column = 0)

output_text = Label(window, text = "Output: ")
output_text.grid(row = 3, column = 0)

def btn1_clicked():
    temp = input1.get()
    output1 = list(permutations(temp))
    output_text.configure(text = "Output: " + str(output1), wrap=195)

btn1 = Button(window, text = "Calculate Combinations", command=btn1_clicked )
btn1.grid(row = 1, column = 1)

window.mainloop()

Python版本3.8


Tags: textinpyselfoutputtkinterconfigureline
1条回答
网友
1楼 · 发布于 2024-03-28 22:15:23

最简单的解决方案是使用python的pprint模块为您格式化数据。或者,您可以编写自己的函数来进行格式化。Tkinter本身不支持格式化数据

例如

import pprint
...
text = pprint.pformat(output1, indent=4)
output_text.configure(text = "Output: " + text, wrap=195)

相关问题 更多 >