英文:
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:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论