【问题标题】:Listbox output Python列表框输出 Python
【发布时间】:2021-12-20 09:03:19
【问题描述】:

晚上好。让我们开始吧:我第一次开始在 Tkinter 中使用 Listbox,我需要从每个值中获取一些操作。例如:用户点击“使用加密”,他得到一个与加密相关的动作,如果他点击“使用解密”,他得到第二个与加密无关的动作。这怎么可能实现?代码如下:

from tkinter import *

window = Tk()

window.title('Login')
window.geometry('300x200')

# This Error:
def crypt():                               
    r = (lis.get(lis.curselection))
    c = (lis.get(lis.curselection))

    if r == r:
        print('Hello')
    
    if c == c:
       print('World')


r = ['Use encrypted']
c = ['Use decrypted']
lis = Listbox(window, selectmode=SINGLE, width=30, height=2)
lis.grid()
for i in r:
    lis.insert(END, i)  
for i in c:
    lis.insert(END, i)


button = Button(window, text='Enter', command=crypt)
button.grid(pady=30)



window.mainloop()

【问题讨论】:

  • if r == r。在那一点上,为什么还要打扰 if 呢? r == r 始终是 True

标签: python python-3.x tkinter listbox output


【解决方案1】:

代码在单击按钮时引发错误。 因为你错过了调用curselection函数的括号

(lis.get( lis.curselection() ))

【讨论】:

  • 是的,我真的忘记调用函数了,但是代码同时运行,同时输出不同的动作。
  • 我已经解决了这个问题,只是决定与else结合。感谢您的帮助!
猜你喜欢
  • 2021-06-11
  • 1970-01-01
  • 2014-01-29
  • 1970-01-01
  • 2013-02-04
  • 1970-01-01
  • 1970-01-01
  • 2018-06-16
  • 1970-01-01
相关资源
最近更新 更多