【问题标题】:How to get these tkinter scrollbars working?如何让这些 tkinter 滚动条工作?
【发布时间】:2017-08-20 03:07:15
【问题描述】:

您好,这些滚动条无法正常工作,即使此代码来自此站点上的一位非常高级的用户,我已经尝试了所有方法。没有错误,他们只是不显示

import tkinter as tk


#Make Window
root = tk.Tk()
root.geometry("612x417")
root.title("Exchange Rates")
root.resizable(0,0)
root.configure(background='lightgrey')
#End

#Create listboxes for currency 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)

#Try to create a scroll bar
scrollbar1 = tk.Scrollbar(root, orient="vertical", command=listbox1.yview)
listbox1.configure(yscrollcommand=scrollbar1.set)

scrollbar2 = tk.Scrollbar(root, orient="vertical", command=listbox2.yview)
listbox2.configure(yscrollcommand=scrollbar2.set)

listbox1.place(x=300,y=50)
listbox2.place(x=300,y=125)



scrollbar3 = Scrollbar(root)
scrollbar3.pack(side="right", fill="y")

listbox = Listbox(root, yscrollcommand=scrollbar3.set)
listbox.pack()

scrollbar3.config(command=listbox.yview)

root.mainloop()

【问题讨论】:

  • 那些是我最接近成功的地方
  • 由于此错误,此代码无法运行:NameError: name 'Scrollbar' is not defined。当我修复它时,我得到NameError: name 'Listbox' is not defined。这些错误是您要问的吗?此外,您的列表框是空的——您认为滚动条会做什么?它无法滚动任何内容,因为没有可滚动的内容。
  • 不,实际上我得到的错误为零所有列表框都有值并且在正确的位置,但只是没有滚动条,它也不是完整的代码,因为我试图保持简短。完整代码在这里 pastebin.com/zB1MVmKC
  • 如果那个链接失效了,那就换一个新的pastebin.com/Ns2AijMW
  • 我们不关心您的真实代码。我们关心能够准确重现您的问题的最小代码。如果您想对您的实际代码进行代码审查,请发帖至code review

标签: python-3.x tkinter listbox scrollbar


【解决方案1】:

我不知道您是如何成功运行它而没有出现错误的,因为您将 tkinter 作为 tk 导入,但是对于列表框,您放置了 Listbox(不是 tk.Listbox),或者对于滚动条 3,您放置了 Scrollbar(不是 tk.Scrollbar)。他们也没有出现,因为你还没有打包/放置它们!

而且...您必须使用任何地方、包装或网格,您不能将它们一起使用。您将.place() 用于列表框1 和2,但随后将.pack() 用于滚动条3 和列表框。无论您首先使用什么(这里是它的位置)都可以工作,但其他人根本不会出现。

【讨论】:

  • 将尝试停止让 n00b 尝试不同的排列方式
【解决方案2】:

希望下面的脚本应该向您展示Scrollbars 如何以清晰简洁的方式工作,以及它们如何受到输入listbox 的项目数量的影响

from tkinter import *

class App:
    def __init__(self, master):
        self.master = master
        self.top = Toplevel(self.master)
        self.frame = Frame(self.top)
        self.entry = Entry(self.master)
        self.button = Button(self.master, text="Ok", command=self.command)
        self.entry.pack()
        self.button.pack()
        self.top.withdraw()
        self.frame.pack()
    def command(self):
        self.frame.destroy()
        self.frame = Frame(self.top)
        self.listbox = Listbox(self.frame)
        self.scroll = Scrollbar(self.frame, orient="vertical", command=self.listbox.yview)
        self.listbox.configure(yscrollcommand=self.scroll.set)
        for i in range(int(self.entry.get())):
            self.listbox.insert(END, "Col"+str(i))
        self.frame.pack()
        self.listbox.pack(side="left")
        self.scroll.pack(side="left", expand=True, fill=Y)
        self.top.deiconify()

root = Tk()
app = App(root)
root.mainloop()

另外请注意,为了让我们审查问题并帮助您解决问题,我们需要能够以简单易懂的方式运行和审查代码。

【讨论】:

  • 是的,我一直在想办法为您优化我的问题。
  • Could not get code to run application window opens but nt 响应在我的 Spyder 和 Python 3.6 配置中没有太多运气,在此代码或其他代码中包含任何“自我”内容
  • 我可以向你保证,如果你在 Python 3.6 IDE 中运行它,它将工作
【解决方案3】:

大家好,回答我自己的问题的人,这是一个在 Frame 中工作的 Listbox 和一个 Scrollbar

import tkinter as tk

#Make Window
root = tk.Tk()
root.geometry("612x417")
root.title("Exchange Rates")
root.resizable(0,0)
root.configure(background='lightgrey')
#End

#Try to create a listbox with a scroll bar within a frame

#Create elements
frame = tk.Frame(root, bd=1, relief='sunken', width=150, height=300)
scrollbar = tk.Scrollbar(frame)
listbox = tk.Listbox(frame)

#Attach listbox to scrollbar
listbox.config(yscrollcommand=scrollbar.set)
scrollbar.config(command=listbox.yview)

#Poulate listbox
for i in range(100):
    listbox.insert('end', i)

#Pack elements
frame.pack(side='top')
scrollbar.pack(side='right', fill='y')
listbox.pack()

root.mainloop()

【讨论】:

    猜你喜欢
    • 2021-12-06
    • 2019-01-19
    • 1970-01-01
    • 2012-04-28
    • 2011-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多