python - Tkinter - 如何限制文本使用的空间?

标签 python python-3.x tkinter

我正在尝试创建一个阶乘计算器 GUI。 该程序工作正常,但我遇到的问题是,当输出中有太多数字时,屏幕会自动增加宽度。我尝试使用 tk.Text 创建文本框大小的限制,以便在填充列时文本继续到下一行。

但是当我必须在 tk.Text 中输入文本时,它不起作用,因为我使用的变量正在按下按钮时调用的函数中进行处理。我尝试过谷歌搜索这个问题,但我找不到任何东西,我确实找到了一些人解释如何使用在函数内部创建/处理的变量,但这不起作用,所以我认为我在我的方法中做错了代码。

注意:我使用 lambda 来调用我的函数(不确定这是否重要)。

TLDR:当输出太多信息时,文本会变得太长。 tk.Text 对我不起作用,因为我无法弄清楚如何使用在仅在按下按钮时调用的函数内部创建/处理的变量。

这是我的完整代码:https://pastebin.com/1MkdRjVE

我的函数代码:

def start_calc():
    output_array = ["placehold"]
    start_text.set("Loading...")
    i = 1
    global e1
    global e2
    output_array.clear()
    string = e1.get()
    string2 = e2.get()
    integr = int(string)
    integr2 = int(string2)
    if string == "":
        error_message.set("Please enter correct numbers.")
    elif string2 == "":
        error_message.set("Please enter correct numbers.")
    else:
        while integr2 >= i:
            calc = integr ** i
            calcstr = (str(calc))
            output_array.append(calcstr)
            i += 1

    start_text.set("Start!")
    output_array_str = (', '.join(output_array))
    output_msg.set("Output: " + output_array_str)
    print(output_array_str) #This is just so I know if it's working or not in the terminal

我的输出代码:

output_msg = tk.StringVar()
output_text = tk.Label(root, textvariable=output_msg, font="Raleway")
output_msg.set("Output: ")
output_text.grid(columnspan=3, column=0, row=14)

最佳答案

我认为这就是您正在寻找的:

#Imports
import tkinter as tk

#Variables
root = tk.Tk()


#Tkinter GUI setup basic
canvas = tk.Canvas(root, width= 400, height=400)
canvas.grid(columnspan=3, rowspan=120)

#Title
text = tk.Label(root, text="Calculating factorials", font="Raleway")
text.grid(column=1, row=1)

#Function
def start_calc():
    output_array = ["", ""]
    start_text.set("Loading...")
    i = 1
    global e1
    global e2
    output_array.clear()
    string = e1.get() 
    string2 = e2.get()
    integr = int(string)
    integr2 = int(string2)
    if string == "":
        error_message.set("Please enter correct numbers.")
    elif string2 == "":
        error_message.set("Please enter correct numbers.")
    else:
        while integr2 >= i:
            calc = integr ** i
            calcstr = (str(calc))
            output_array.append(calcstr)
            i += 1   
    start_text.set("Start!")
    output_array_str = (', '.join(output_array))
    # Change the output
    output_text.config(state="normal")
    # delete last output:
    output_text.delete("0.0", "end")
    # insert new output:
    output_text.insert("end", output_array_str)
    output_text.config(state="disabled")
    print(output_array_str) #This is just so I know if it's working or not in the terminal
    
    
#input
tk.Label(root, text="Number :").grid(row=10)
tk.Label(root, text="Factorial :").grid(row=11)
e1 = tk.Entry(root)
e2 = tk.Entry(root)
e1.grid(row=10, column=1)
e2.grid(row=11, column=1)

#Error message if the input is invalid
error_message = tk.StringVar()
error_text = tk.Label(root, textvariable=error_message, font="Raleway")
error_message.set(" ")
error_text.grid(column=1, row=12)

#Startbutton
start_text = tk.StringVar()
start_btn = tk.Button(root, textvariable=start_text, command=start_calc, font="Raleway", bg="#20bebe", fg="white", height=2, width=15)
start_text.set("Start!")
start_btn.grid(column=1, row=13, pady=10)

#output
output_text = tk.Text(root, height=1, width=20, wrap="none", font="Raleway")
output_text.insert("end", "Output")
output_text.config(state="disabled")
output_text.grid(columnspan=3, column=0, row=14, sticky="news")

#Adding a scrollbar
scrollbar = tk.Scrollbar(root, orient="horizontal", command=output_text.xview)
scrollbar.grid(columnspan=3, column=0, row=15, sticky="news")
output_text.config(xscrollcommand=scrollbar.set)

#disclaimer message
disclaimer_text = tk.Label(root, text="Disclaimer: The factorials will be printed from 1 to the number you entered.")
disclaimer_text.grid(columnspan=3, column=0, row=110)

root.mainloop()

我用了<tkinter.Text>小部件 wrap="none" , height=1width=20制作输出框。我禁用了该条目,以便用户无法更改结果但仍可以复制它。

关于python - Tkinter - 如何限制文本使用的空间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66321605/

相关文章:

python - Pandas 数据框 - 具有包含字符串列表的列

Python3 - 将变量的值插入另一个变量命令

python - 为什么这个 tkinter 菜单小部件的一部分不显示?

python - 在漫长的过程中与 Tkinter 窗口交互

python - 无法将图像添加到 Windows 上的 GUI(tkinter)

python - 如何在pytest中集成对readme的检查

c++ - Python 提示 SWIG 模块不存在

python - 将子列表添加到一个列表中作为python中的列表元素

python - 为什么 'pip install mysqlclient' 在 ubuntu 18.04 LTS 中不工作

Python:在派生类中隐藏基类的成员