Python 3.x 如何设置列表框tkinter的标签

Python 3.x 如何设置列表框tkinter的标签,python-3.x,select,tkinter,listbox,listboxitem,Python 3.x,Select,Tkinter,Listbox,Listboxitem,您好,我正在尝试根据列表框选择设置一个标签,该列表框选择基于我在这里发现的一些其他问题 下面是我正在使用的相关代码。请原谅,如果我没有正确发布,我试图发布的只是要求的相关代码,这一切运行时,除了我运行它时得到的错误 错误为:print'您选择的项目%d:“%s”,%(索引,值) 语法错误:无效语法 #Show selected currency for from in label frmcur_text = tk.StringVar() frmcur = tk.Label(root, textv

您好,我正在尝试根据列表框选择设置一个标签,该列表框选择基于我在这里发现的一些其他问题

下面是我正在使用的相关代码。请原谅,如果我没有正确发布,我试图发布的只是要求的相关代码,这一切运行时,除了我运行它时得到的错误

错误为:
print'您选择的项目%d:“%s”,%(索引,值)
语法错误:无效语法

#Show selected currency for from in label
frmcur_text = tk.StringVar()
frmcur = tk.Label(root, textvariable=frmcur_text, font="Helvetica 10 bold", anchor='w', background='lightgrey').place(x=195,y=50)

def onselect(event):
    # Note here that Tkinter passes an event object to onselect()

    w = event.Listbox
    index = int(w.curselection()[0])
    value = w.get(index)
    print 'You selected item %d: "%s"', % (index, value)

#Create listboxes for xurrency selection
listbox1 = tk.Listbox(root, font="Helvetica 11 bold", height=3, width=10)
listbox2 = tk.Listbox(root, font="Helvetica 11 bold", height=3, width=10)


listbox1.bind('<<ListboxSelect>>', onselect)    

cs = listbox1.curselection()

frmcur_text.set(cs)
#在标签中显示从中选择的货币
frmcur_text=tk.StringVar()
frmcur=tk.Label(root,textvariable=frmcur_text,font=“Helvetica 10 bold”,anchor='w',background='lightgrey')。放置(x=195,y=50)
def onselect(事件):
#请注意,Tkinter将事件对象传递给onselect()
w=事件列表框
index=int(w.curselection()[0])
值=w.get(索引)
打印“您选择的项目%d:“%s”,%(索引,值)
#为xurrency选择创建列表框
listbox1=tk.Listbox(root,font=“Helvetica 11 bold”,高度=3,宽度=10)
listbox2=tk.Listbox(root,font=“Helvetica 11 bold”,高度=3,宽度=10)
listbox1.bind(“”,onselect)
cs=listbox1.curselection()
frmcur_文本集(cs)

Spyder和Python 3.6答案

import tkinter as tk
root = tk.Tk()
root.geometry("612x417")
root.title("change label on listbox selection")
root.resizable(0,0)
root.configure(background='lightgrey')


#Show selected currency for from in label
frmcur_text = tk.StringVar()
frmcur = tk.Label(root, textvariable=frmcur_text, font="Helvetica 10 bold", anchor='w', background='lightgrey').place(x=195,y=50)

def onselect(evt):
    # Note here that Tkinter passes an event object to onselect()

    w = evt.widget
    index = int(w.curselection()[0])
    value = w.get(index)
#    print ('You selected item %d: "%s"' % (index, value))
    frmcur_text.set(value)

#Create listboxes for xurrency selection
listbox1 = tk.Listbox(root, font="Helvetica 11 bold", height=3, width=10)
listbox2 = tk.Listbox(root, font="Helvetica 11 bold", height=3, width=10)
listbox1.place(x=300,y=50)
listbox2.place(x=300,y=125)


for i in range(20):
    i = i + 1
    listbox1.insert(1, i)
    listbox2.insert(1, i)


listbox1.bind('<<ListboxSelect>>', onselect)    

cs = listbox1.curselection()

frmcur_text.set(cs)

root.mainloop()
将tkinter作为tk导入
root=tk.tk()
根几何(“612x417”)
root.title(“更改列表框选择上的标签”)
根目录。可调整大小(0,0)
root.configure(background='lightgrey')
#在标签中显示从中选择的货币
frmcur_text=tk.StringVar()
frmcur=tk.Label(root,textvariable=frmcur_text,font=“Helvetica 10 bold”,anchor='w',background='lightgrey')。放置(x=195,y=50)
def onselect(evt):
#请注意,Tkinter将事件对象传递给onselect()
w=evt.widget
index=int(w.curselection()[0])
值=w.get(索引)
#打印('您选择了项目%d:“%s”%”(索引,值))
frmcur_text.set(值)
#为xurrency选择创建列表框
listbox1=tk.Listbox(root,font=“Helvetica 11 bold”,高度=3,宽度=10)
listbox2=tk.Listbox(root,font=“Helvetica 11 bold”,高度=3,宽度=10)
列表框1.位置(x=300,y=50)
列表框2.位置(x=300,y=125)
对于范围(20)内的i:
i=i+1
列表框1.插入(1,i)
列表框2.插入(1,i)
listbox1.bind(“”,onselect)
cs=listbox1.curselection()
frmcur_文本集(cs)
root.mainloop()

Spyder和Python 3.6答案

import tkinter as tk
root = tk.Tk()
root.geometry("612x417")
root.title("change label on listbox selection")
root.resizable(0,0)
root.configure(background='lightgrey')


#Show selected currency for from in label
frmcur_text = tk.StringVar()
frmcur = tk.Label(root, textvariable=frmcur_text, font="Helvetica 10 bold", anchor='w', background='lightgrey').place(x=195,y=50)

def onselect(evt):
    # Note here that Tkinter passes an event object to onselect()

    w = evt.widget
    index = int(w.curselection()[0])
    value = w.get(index)
#    print ('You selected item %d: "%s"' % (index, value))
    frmcur_text.set(value)

#Create listboxes for xurrency selection
listbox1 = tk.Listbox(root, font="Helvetica 11 bold", height=3, width=10)
listbox2 = tk.Listbox(root, font="Helvetica 11 bold", height=3, width=10)
listbox1.place(x=300,y=50)
listbox2.place(x=300,y=125)


for i in range(20):
    i = i + 1
    listbox1.insert(1, i)
    listbox2.insert(1, i)


listbox1.bind('<<ListboxSelect>>', onselect)    

cs = listbox1.curselection()

frmcur_text.set(cs)

root.mainloop()
将tkinter作为tk导入
root=tk.tk()
根几何(“612x417”)
root.title(“更改列表框选择上的标签”)
根目录。可调整大小(0,0)
root.configure(background='lightgrey')
#在标签中显示从中选择的货币
frmcur_text=tk.StringVar()
frmcur=tk.Label(root,textvariable=frmcur_text,font=“Helvetica 10 bold”,anchor='w',background='lightgrey')。放置(x=195,y=50)
def onselect(evt):
#请注意,Tkinter将事件对象传递给onselect()
w=evt.widget
index=int(w.curselection()[0])
值=w.get(索引)
#打印('您选择了项目%d:“%s”%”(索引,值))
frmcur_text.set(值)
#为xurrency选择创建列表框
listbox1=tk.Listbox(root,font=“Helvetica 11 bold”,高度=3,宽度=10)
listbox2=tk.Listbox(root,font=“Helvetica 11 bold”,高度=3,宽度=10)
列表框1.位置(x=300,y=50)
列表框2.位置(x=300,y=125)
对于范围(20)内的i:
i=i+1
列表框1.插入(1,i)
列表框2.插入(1,i)
listbox1.bind(“”,onselect)
cs=listbox1.curselection()
frmcur_文本集(cs)
root.mainloop()

后面紧跟着一个
%
,这确实是非法语法。您需要删除
@BryanOakley嗨,Bryan我的代码快写完了,想知道您是否愿意帮我完成一些逻辑混乱,我不想在这里发布太长时间,很有价值,但解释起来有点混乱,也许你可以打开一个私人聊天室,或者给我一个电子邮件地址,这样我就可以给你发一个链接或者一个.py,当然是你喜欢的。如果你没有时间,或者只是不想担心,那么请先帮帮忙!!!在
后面紧跟着一个
%
,这确实是非法语法。您需要删除
@BryanOakley嗨,Bryan我的代码快写完了,想知道您是否愿意帮我完成一些逻辑混乱,我不想在这里发布太长时间,很有价值,但解释起来有点混乱,也许你可以打开一个私人聊天室,或者给我一个电子邮件地址,这样我就可以给你发一个链接或者一个.py,当然是你喜欢的。如果你没有时间,或者只是不想担心,那么请先帮帮忙!!!