【问题标题】:How to set label on curselection of Listbox tkinter如何在 Listbox tkinter 的光标上设置标签
【发布时间】:2017-08-21 18:26:13
【问题描述】:

您好,我正在尝试根据我在此处找到的其他一些问题根据列表框选择设置标签。

以下是我正在使用的相关代码。如果我没有正确发布,请原谅我正在尝试仅按要求发布相关代码,除了运行时出现的错误之外,所有这些都运行。

错误是:print 'You selected item %d: "%s"', % (index, value) SyntaxError: invalid syntax

#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)

【问题讨论】:

  • % 紧跟在, 之后,这确实是非法语法。您需要删除,
  • @BryanOakley 嗨 Bryan 我的代码差不多完成了,想知道您是否愿意帮我解决一些逻辑混乱,我不想在这里发布太长、有价值和有点令人困惑的解释也许你可以打开一个私人聊天或轻弹我的电子邮件地址,这样我就可以通过电子邮件向你发送链接或 .py 当然你的偏好。如果您没有时间或只是不想担心,请寻求以前的帮助!!!

标签: python-3.x select tkinter listbox listboxitem


【解决方案1】:

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()

【讨论】:

    猜你喜欢
    • 2016-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-30
    • 2013-09-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多