英文:
With tkinter text widget, how to use .insert so that it overwrites the entire text content?
问题
我正在使用SQL表和tkinter构建餐厅菜单,其中包含有关头盘、主菜和甜点的按钮,但由于某种原因停留在头盘页面:
# TKinter设置
import tkinter as tk
w = tk.Tk()
w.title("Interactive Restaurant Menu")
w.geometry("640x640")
w.resizable(False, False)
w['bg'] = 'black'
# SQL设置
import mysql.connector as mc
con = mc.connect(user='root', host='localhost', password='Cars!234', database='Zayan')
cur = con.cursor()
# 打印菜单项目
v = tk.Scrollbar(orient='vertical')
list = tk.Text(bg='black', font=('Baskerville', 18), wrap=tk.WORD, yscrollcommand=v.set)
list.grid(column=0, row=2, columnspan=4, padx=20)
def getItem(ID, course):
currentItem = []
table_name = "Menu" + course
query = "SELECT Item,Price,Description FROM {} WHERE ID = %s".format(table_name)
cur.execute(query, (ID,))
result = cur.fetchone()
currentItem.append(result[0] + ' (' + str(result[1]) + '$)')
currentItem.append(result[2])
return currentItem
def printItems(course):
cur.execute("select max(ID) from {}".format("Menu" + course))
n = cur.fetchone()
for i in range(1, n[0] + 1):
list.insert(tk.END, '\n\n' + str(i) + "." + getItem(i, course)[0] + '\n' + getItem(i, course)[1])
list.config(state='disabled', highlightthickness=0)
# 标题和按钮
head = tk.Label(text="Menu", font=("Garamond", 40), bg='black')
head.grid(column=0, row=0, columnspan=4, sticky='N', pady=10)
app = tk.Button(text="Entrees", width=12, height=2, command=lambda: printItems('Entree'))
main = tk.Button(text="Mains", width=12, height=2, command=lambda: printItems('Main'))
des = tk.Button(text="Desserts", width=12, height=2)
deal = tk.Button(text="Deals", width=12, height=2)
app.grid(column=0, row=1, padx=8)
main.grid(column=1, row=1, padx=8)
des.grid(column=2, row=1, padx=8)
deal.grid(column=3, row=1, padx=8)
w.grid_columnconfigure((0, 1, 2, 3), weight=1)
w.mainloop()
我认为问题出在文本小部件的.insert
使用上。是否有其他替代方法?
英文:
I am building a restaurant menu using sql tables and tkinter which has buttons for Entrees, Mains and Desserts but it is stuck on the Entrees page for some reason:
#Setup - TKinter
import tkinter as tk
w = tk.Tk()
w.title("Interactive Restaurant Menu")
w.geometry("640x640")
w.resizable(False,False)
w['bg'] = 'black'
#Setup - SQL
import mysql.connector as mc
con = mc.connect(user = 'root',host = 'localhost', password = 'Cars!234',database = 'Zayan')
cur = con.cursor()
#Printing Menu Items
v = tk.Scrollbar( orient='vertical')
list = tk.Text(bg = 'black',font = ('Baskerville', 18),wrap = tk.WORD,yscrollcommand = v.set)
list.grid(column = 0,row = 2, columnspan = 4,padx = 20)
def getItem(ID, course):
currentItem = []
table_name = "Menu" + course
query = "SELECT Item,Price,Description FROM {} WHERE ID = %s".format(table_name)
cur.execute(query, (ID,))
result = cur.fetchone()
currentItem.append(result[0] + ' (' + str(result[1]) + '$)')
currentItem.append(result[2])
return currentItem
def printItems(course):
cur.execute("select max(ID) from {}".format("Menu" + course))
n = cur.fetchone()
for i in range(1,n[0] + 1):
list.insert(tk.END,'\n\n' + str(i) + "." + getItem(i,course)[0] + '\n' + getItem(i,course)[1])
list.config(state='disabled',highlightthickness=0)
#Title And Buttons
head = tk.Label(text = "Menu",font = ("Garamond",40),bg = 'black')
head.grid(column = 0,row = 0,columnspan = 4, sticky = 'N', pady = 10)
app = tk.Button(text="Entrees",width = 12,height = 2,command=printItems('Entree'))
main = tk.Button(text="Mains",width = 12,height = 2,command=printItems('Main'))
des = tk.Button(text="Desserts",width = 12,height = 2)
deal = tk.Button(text="Deals",width = 12,height = 2)
app.grid(column = 0, row = 1,padx = 8)
main.grid(column = 1, row = 1,padx = 8)
des.grid(column = 2, row = 1,padx = 8)
deal.grid(column = 3, row = 1,padx = 8)
w.grid_columnconfigure((0,1,2,3), weight=1)
w.mainloop()
I think the problem lies with usage of .insert for the text widget. What can I use alternatively?
答案1
得分: 1
使用tkinter文本小部件,如何使用.insert以覆盖整个文本内容?
与delete
方法结合使用。首先删除所有内容,然后插入新文本。
或者,使用replace
方法,它正好做它所说的事情。它用一些新文本替换文本范围。您必须为要删除的文本范围提供起始和结束索引,并包括新文本。
英文:
> With tkinter text widget, how to use .insert so that it overwrites the entire text content?
Use it in combination with the delete
method. First delete everything, then insert the new text.
Or, use the replace
method which does exactly what it says. It replaces a range of text with some new text. You must give it both a starting and ending index of the range of text to delete, and also include the new text.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论