在python中,如何使用滚动条和按钮选择用户在列表中单击的行?

2024-09-28 23:29:46 发布

您现在位置:Python中文网/ 问答频道 /正文

在python中,如何使用滚动条和按钮选择用户在列表中单击的行? 我试着用按钮的命令来定义函数Get。。。我有线索吗。 这是我目前为止的代码:

from tkinter import *

def get():
    userline=leftside.get(line)
    print(userline) 

def scale():
    thescale=Tk()
    scroll=Scrollbar(thescale)
    scroll.pack(side=RIGHT, fill=Y)

    leftside = Listbox(thescale, yscrollcommand = scroll.set)
    for line in range(101):
        leftside.insert(END, "Scale "+str(line))

    leftside.pack(side=LEFT, fill=BOTH)
    scroll.config(command=leftside.yview)

    selectbutton=Button(thescale, text="Select", command=get)
    selectbutton.pack()

    thescale.mainloop()

scale()

Tags: 用户getdeflinefill按钮sidecommand
1条回答
网友
1楼 · 发布于 2024-09-28 23:29:46

Listbox小部件作为“活动”索引,与用户上次单击的项相对应。所以leftside.get('active')会给你想要的东西。另外,如果将get方法移出scale,则需要使leftside成为全局变量,否则不能在get内使用它。在

from tkinter import Tk, Listbox, Button, Scrollbar

def get():
    userline=leftside.get('active')
    print(userline) 

def scale():
    global leftside
    thescale=Tk()
    scroll=Scrollbar(thescale)
    scroll.pack(side='right', fill='y')

    leftside = Listbox(thescale, yscrollcommand = scroll.set)
    for line in range(101):
        leftside.insert('end', "Scale "+str(line))

    leftside.pack(side='left', fill='both')
    scroll.config(command=leftside.yview)

    selectbutton=Button(thescale, text="Select", command=get)
    selectbutton.pack()

    thescale.mainloop()

scale()

相关问题 更多 >