每行文本部件的开头都有双倍空格,无论插入什么内容。

huangapple go评论67阅读模式
英文:

How do I make it so that every line of a text widget has a double space in the beginning no matter what else gets inserted in it?

问题

I am building a restaurant menu in python using tkinter and I noticed that my menu items are too close to the edge of the text widget. How do I make it so that every line has some space at the start?

Relevant code:

items = tk.Text(bg='black', font=('Baskerville', 18), wrap=tk.WORD, height=20,)
items.grid(column=0, row=2, columnspan=4, padx=15, pady=10)

# Print Menu Items
def printItems(course):
    global exportCourse
    exportCourse = course
    items.config(state='normal')

    # Adjust Menu Items According To Dietary Preferences
    query = 'Select Item,Price,Description FROM {} where Item is Not Null'
    if vegan_choice_intvar.get() == 1:
        query += ' and Vegan = "Yes"'
    if veg_choice_intvar.get() == 1:
        query += ' and Vegetarian = "Yes"'
    if soy_choice_intvar.get() == 1:
        query += ' and ContainsSoy = "No"'
    if nuts_choice_intvar.get() == 1:
        query += ' and ContainsNuts = "No"'
    if milk_choice_intvar.get() == 1:
        query += ' and ContainsMilk = "No"'
    if fish_choice_intvar.get() == 1:
        query += ' and ContainsFish = "No"'

    # Retrieve Item Count
    cur.execute("select count(Item) from {}".format("Menu" + course))
    n = cur.fetchone()[0]

    # Check If List Is Empty
    if n == 0:
        items.delete(1.0, 'end')
        items.insert(tk.END, '\n\n\n\n\n\n\n\n\n\n                                                        No Items Here')
        items.config(state='disabled', highlightthickness=2, highlightbackground='white', highlightcolor='white')
        return

    # Retrieve Item Details
    cur.execute(query.format('Menu' + course))

    # Assemble Details And Print
    items.config(state='normal')
    items.delete(1.0, 'end')
    for i in range(1, n + 1):
        result = cur.fetchone()
        items.insert(tk.END, '\n\n' + str(i) + ". " + result[0] + ' (' + str(result[1]) + '$)' + '\n' + result[2])
    items.config(state='disabled', highlightthickness=2, highlightcolor='white', highlightbackground='white')

Current output:

每行文本部件的开头都有双倍空格,无论插入什么内容。

英文:

I am building a restaurant menu in python using tkinter and I noticed that my menu items are too close to the edge of the text widget. How do I make it so that every line has some space at the start?

Relevant code:

items = tk.Text(bg = 'black',font = ('Baskerville', 18),wrap = tk.WORD,height = 20,)
items.grid(column = 0,row = 2, columnspan = 4,padx = 15,pady = 10)
# Print Menu Items
def printItems(course):
global exportCourse
exportCourse = course
items.config(state='normal')
# Adjust Menu Items According To Dietary Preferences
query = 'Select Item,Price,Description FROM {} where Item is Not Null'
if vegan_choice_intvar.get() == 1:
query += ' and Vegan = "Yes"'
if veg_choice_intvar.get() == 1:
query += ' and Vegetarian = "Yes"'
if soy_choice_intvar.get() == 1:
query += ' and ContainsSoy = "No"'
if nuts_choice_intvar.get() == 1:
query += ' and ContainsNuts = "No"'
if milk_choice_intvar.get() == 1:
query += ' and ContainsMilk = "No"'
if fish_choice_intvar.get() == 1:
query += ' and ContainsFish = "No"'
# Retrieve Item Count
cur.execute("select count(Item) from {}".format("Menu" + course))
n = cur.fetchone()[0]
#Check If List Is Empty
if n == 0:
items.delete(1.0,'end')
items.insert(tk.END,'\n\n\n\n\n\n\n\n\n\n                                                        No Items Here')
items.config(state='disabled',highlightthickness=2,highlightbackground='white',highlightcolor='white')
return
# Retrieve Item Details
cur.execute(query.format('Menu' + course))
# Assemble Details And Print
items.config(state = 'normal')
items.delete(1.0,'end')
for i in range(1,n + 1):
result = cur.fetchone()
items.insert(tk.END,'\n\n' + str(i) + ". " + result[0] + ' (' + str(result[1]) + '$)' + '\n' + result[2])
items.config(state='disabled',highlightthickness=2,highlightcolor='white',highlightbackground='white')

Current output:

每行文本部件的开头都有双倍空格,无论插入什么内容。

答案1

得分: 1

如果您希望在文本框的左侧添加一些边距,您可以使用.tag_config()来设置边距并在每个.insert()中添加标签:

...
# 使用标签“lmargins”设置边距
items.tag_config("lmargins", lmargin1=20, lmargin2=20)

def printItems(course):
    ...
    for i in range(1, n+1):
        ...
        # 添加标签“lmargins”
        items.insert(tk.END, '\n\n'+str(i)+". "+result[0]+' ('+str(result[1])+'$)'+'\n'+result[2], "lmargins")
    ...

示例结果:

每行文本部件的开头都有双倍空格,无论插入什么内容。

英文:

If you want some margin at the left side of the text box, you can use .tag_config() to set up the margins and add the tag in every .insert():

...
# setup margins using tag "lmargins"
items.tag_config("lmargins", lmargin1=20, lmargin2=20)

def printItems(course):
    ...
    for i in range(1, n+1):
        ...
        # add the tag "lmargins"
        items.insert(tk.END, '\n\n'+str(i)+". "+result[0]+' ('+str(result[1])+'$)'+'\n'+result[2], "lmargins")
    ...

Sample result:

每行文本部件的开头都有双倍空格,无论插入什么内容。

huangapple
  • 本文由 发表于 2023年7月11日 13:41:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76658955.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定