【问题标题】:How can I add spacing each time I insert text into a textbox in tkinter?每次在 tkinter 中将文本插入文本框时,如何添加间距?
【发布时间】:2020-11-23 04:27:56
【问题描述】:

每次我调用文本框的插入时,我一直在摆弄在文本之间添加间距。我注意到的是,当我多次调用它时,已经添加了间距,但是当我有 2 行或更多行 .insert 全部被单击时,当我单击一个按钮时,文本全部混合在一起,中间没有间距。下面附上问题的图片,这是我的代码。

taskList.insert("1.end", siteTaskID)
taskList.insert("1.end",siteTaskTXT)

siteTaskID 和 siteTaskTXT 是 2 个变量,每个变量都包含字符串。字符串是根据用户输入决定的。例如,这些变量可以是

siteTaskID = "HELLO"
siteTaskTXT = "Store"

这实际上是我为执行它而编写的所有必要代码。我省略了字符串转换,也省略了调用这个函数的按钮。

This is when I call both with the code above

This is when I remove the second .insert function and click the button multiple times (this is what I want to happen when I call both at the same time.)

【问题讨论】:

  • siteTaskID 这些只是包含字符串的变量吗?对我来说,使用'1.end' 会引发错误bad entry index "1.end"
  • “间距”是什么意思?你想这样做tb.insert('1.end', ' ')吗? (@CoolCloud 作者可能在使用 tkinter.Text,而您可能正在使用 tkinter.Entry。)
  • @哦好的,谢谢你的说明,我很笨
  • 好吧,我不知道这是否有帮助,但试着说,taskList.insert("1.end", siteTaskID.ljust(6) taskList.insert("1.end",siteTaskTXT.ljust(6))
  • 您发布的代码没有任何与“在文本之间添加间距” 相关的内容。请提供minimal reproducible example

标签: python tkinter text spacing


【解决方案1】:

只需使用fstring 并在两个部分之间放置任意数量的空格。

taskList.insert("1.end", f'{siteTaskID} {siteTaskTXT}')

编辑: 您出于某种原因拒绝发布 MRE,因此,请注意这个黑暗中的镜头

from tkinter import Tk, Text, Button 


class App(Tk):
    WIDTH  = 800
    HEIGHT = 600

    def __init__(self, *args, **kwargs):
        Tk.__init__(self, *args, **kwargs)

        self.textbox = Text(self)
        self.textbox.grid(row=0, column=0, sticky='nswe')

        self.index = 0
        
        #this is the current line you want to affect
        self.cline = 1
        
        #this represents 5 different times you have gathered line data into a list
        #...via a much more dynamic method
        self.vals = [
            ["id1", "text1", "usertext1"],
            ["id2", "text2", "usertext2"],
            ["id3", "text3", "usertext3"],
            ["id4", "text4", "usertext4"],
            ["id5", "text5", "usertext5"],
        ]
        
        #current line data
        self.data = self.vals[self.index]
        
        Button(self, text="click", command=lambda: self.process(self.data)).grid(row=1, column=0)

    #this represents processing a line of gathered data   
    def process(self, data):
        if data: 
            self.textbox.insert(f'{self.cline}.end', f'{" ".join(data)}\n')
        
        #below is only necessary for this example
        #the equivalents should be done somewhere else
        self.cline += 1
        self.index += 1
        self.data = self.vals[self.index] if self.index < len(self.vals) else None


if __name__ == '__main__':
    app = App()
    app.title("My Application")
    app.geometry(f'{App.WIDTH}x{App.HEIGHT}')
    app.mainloop()

一边

从您的评论来看,您似乎试图欺骗一种质量方法,例如一个充满LabelEntry 的表格,用于将Text 小部件格式化为类似表格的显示的低劣方法.如果是这种情况,您可能需要考虑您当前的方法完全是在浪费时间,而且会给您带来比结果更令人头疼的问题。

【讨论】:

  • 这会起作用,但这两个只是在页面上添加更多变量的开始。而且它们中的大多数都是用户输入的,因此长度会发生变化。
  • @omen ~ 这意味着您的示例不是最小可重复的示例。 1.end 表示“第 1 行的结尾”。空间永远不会出现。您将始终必须手动填充输入。尝试第二种方式时出现空格的原因可能是因为您有一些错误,导致 sideTaskID 只能是空格。发布一个包含您所有条件的示例或查看我的编辑并接受它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-11-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-15
  • 1970-01-01
相关资源
最近更新 更多