多次将结果从选项菜单添加到标签
Add many times a result form an optionmenu to a label
我正在学习 python 以及如何使用 tkinter。作为练习,我想在同一个标签中添加选项菜单中的许多结果。因为我没有找到解决方案,所以我每次在菜单中 select 一个新选项时都尝试创建一个具有不同位置的新标签。但它不起作用,看起来我开始了一个无限的过程。
这是我的尝试,但这并不是我想要的。
from tkinter import *
root = Tk()
root.title("Fruit list")
root.geometry("800x700")
root.config(bg = "white")
#Dictionnary:
Fruit= {
'Apple': 'yellow juicy',
'Strawberry': 'red sweet',
'Orange': 'orange juicy',
'Tomato': 'red juicy',
}
def selected(event):
n_num = 0
r_num = 6
c_num = 1
varclicked = clicked.get()
r_fruit = Label(root, text= varclicked)
r_fruit.grid(row=r_num, column=c_num, padx=5, pady=5)
r_fruit.config(bg="white")
n_num +=1
while n_num!=10:
if 1<n_num<4 or 6<n_num<10:
varclicked = clicked.get()
r_fruit = Label(root, text=varclicked)
r_fruit.grid(row=r_num, column=c_num, padx=5, pady=5)
r_fruit.config(bg="white")
n_num += 1
c_num += 1
elif n_num == 5:
r_num += 1
c_num = 1
varclicked = clicked.get()
r_fruit = Label(root, text=varclicked)
r_fruit.grid(row=r_num, column=c_num, padx=5, pady=5)
r_fruit.config(bg="white")
n_num += 1
clicked = StringVar(root)
clicked.set('Choose a Fruit')
lst_fruit = OptionMenu(root, clicked, *Fruit, command=selected)
lst_fruit.grid(row=7, column=0)
root.mainloop()
首先,为了避免无限循环,
确保 n_num 始终递增。
例如:
if (1<=n_num<=4) or (6<=n_num<10):
一个更完整的例子,可能会有帮助:
from tkinter import *
root = Tk()
Fruit= {
'Apple': 'yellow juicy',
'Strawberry': 'red sweet',
'Orange': 'orange juicy',
'Tomato': 'red juicy',
}
l = Label(root, text='')
l.pack()
def selected(event):
prevText = l.cget('text')
sep = '\n' if prevText else ''
addedText = clicked.get()
l.config(text=f'{prevText}{sep}{addedText}')
clicked = StringVar(root, 'Choose a Fruit')
lst_fruit = OptionMenu(
root, clicked, *Fruit, command=selected
)
lst_fruit.pack()
root.mainloop()
我正在学习 python 以及如何使用 tkinter。作为练习,我想在同一个标签中添加选项菜单中的许多结果。因为我没有找到解决方案,所以我每次在菜单中 select 一个新选项时都尝试创建一个具有不同位置的新标签。但它不起作用,看起来我开始了一个无限的过程。
这是我的尝试,但这并不是我想要的。
from tkinter import *
root = Tk()
root.title("Fruit list")
root.geometry("800x700")
root.config(bg = "white")
#Dictionnary:
Fruit= {
'Apple': 'yellow juicy',
'Strawberry': 'red sweet',
'Orange': 'orange juicy',
'Tomato': 'red juicy',
}
def selected(event):
n_num = 0
r_num = 6
c_num = 1
varclicked = clicked.get()
r_fruit = Label(root, text= varclicked)
r_fruit.grid(row=r_num, column=c_num, padx=5, pady=5)
r_fruit.config(bg="white")
n_num +=1
while n_num!=10:
if 1<n_num<4 or 6<n_num<10:
varclicked = clicked.get()
r_fruit = Label(root, text=varclicked)
r_fruit.grid(row=r_num, column=c_num, padx=5, pady=5)
r_fruit.config(bg="white")
n_num += 1
c_num += 1
elif n_num == 5:
r_num += 1
c_num = 1
varclicked = clicked.get()
r_fruit = Label(root, text=varclicked)
r_fruit.grid(row=r_num, column=c_num, padx=5, pady=5)
r_fruit.config(bg="white")
n_num += 1
clicked = StringVar(root)
clicked.set('Choose a Fruit')
lst_fruit = OptionMenu(root, clicked, *Fruit, command=selected)
lst_fruit.grid(row=7, column=0)
root.mainloop()
首先,为了避免无限循环, 确保 n_num 始终递增。 例如:
if (1<=n_num<=4) or (6<=n_num<10):
一个更完整的例子,可能会有帮助:
from tkinter import *
root = Tk()
Fruit= {
'Apple': 'yellow juicy',
'Strawberry': 'red sweet',
'Orange': 'orange juicy',
'Tomato': 'red juicy',
}
l = Label(root, text='')
l.pack()
def selected(event):
prevText = l.cget('text')
sep = '\n' if prevText else ''
addedText = clicked.get()
l.config(text=f'{prevText}{sep}{addedText}')
clicked = StringVar(root, 'Choose a Fruit')
lst_fruit = OptionMenu(
root, clicked, *Fruit, command=selected
)
lst_fruit.pack()
root.mainloop()