框架的 Tkinter 滚动条 [英] Tkinter scrollbar for frame

查看:33
本文介绍了框架的 Tkinter 滚动条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标是向一个包含多个标签的框架添加一个垂直滚动条.一旦框架内的标签超过框架的高度,滚动条就会自动启用.搜索之后,我发现这个很有用邮政.基于那篇文章,我明白为了实现我想要的,(如果我错了,请纠正我,我是初学者)我必须先创建一个 Frame,然后创建一个 Canvas 在该框架内,并将滚动条也粘贴到该框架上.之后,创建另一个框架并将其作为窗口对象放入画布中.所以,我终于想出了这个:

My objective is to add a vertical scroll bar to a frame which has several labels in it. The scroll bar should automatically enabled as soon as the labels inside the frame exceed the height of the frame. After searching through, I found this useful post. Based on that post I understand that in order to achieve what i want, (correct me if I am wrong, I am a beginner) I have to create a Frame first, then create a Canvas inside that frame and stick the scroll bar to that frame as well. After that, create another frame and put it inside the canvas as a window object. So, I finally come up with this:

from Tkinter import *

def data():
    for i in range(50):
       Label(frame,text=i).grid(row=i,column=0)
       Label(frame,text="my text"+str(i)).grid(row=i,column=1)
       Label(frame,text="..........").grid(row=i,column=2)

def myfunction(event):
    canvas.configure(scrollregion=canvas.bbox("all"),width=200,height=200)

root=Tk()
sizex = 800
sizey = 600
posx  = 100
posy  = 100
root.wm_geometry("%dx%d+%d+%d" % (sizex, sizey, posx, posy))

myframe=Frame(root,relief=GROOVE,width=50,height=100,bd=1)
myframe.place(x=10,y=10)

canvas=Canvas(myframe)
frame=Frame(canvas)
myscrollbar=Scrollbar(myframe,orient="vertical",command=canvas.yview)
canvas.configure(yscrollcommand=myscrollbar.set)

myscrollbar.pack(side="right",fill="y")
canvas.pack(side="left")
canvas.create_window((0,0),window=frame,anchor='nw')
frame.bind("<Configure>",myfunction)
data()
root.mainloop()

  1. 我做对了吗?是否有更好/更智能的方法来实现此代码给我的输出?
  2. 为什么我必须使用网格方法?(我尝试了 place 方法,但画布上没有出现任何标签.)
  3. 在画布上创建窗口时使用 anchor='nw' 有什么特别之处?
  1. Am I doing it right? Is there better/smarter way to achieve the output this code gave me?
  2. Why must I use grid method? (I tried place method, but none of the labels appear on the canvas.)
  3. What so special about using anchor='nw' when creating window on canvas?

请简单回答,因为我是初学者.

Please keep your answer simple, as I am a beginner.

推荐答案

我做对了吗?有没有更好/更聪明的方法来实现这段代码给我的输出?"

"Am i doing it right?Is there better/smarter way to achieve the output this code gave me?"

总的来说,是的,你做得对.除了画布之外,Tkinter 没有本机可滚动容器.如您所见,设置起来真的不难.如您的示例所示,只需 5 或 6 行代码即可使其工作 - 取决于您如何计算行数.

Generally speaking, yes, you're doing it right. Tkinter has no native scrollable container other than the canvas. As you can see, it's really not that difficult to set up. As your example shows, it only takes 5 or 6 lines of code to make it work -- depending on how you count lines.

为什么我必须使用网格方法?(我尝试了放置方法,但画布上没有出现任何标签?)"

"Why must i use grid method?(i tried place method, but none of the labels appear on the canvas?)"

你问为什么必须使用网格.没有要求使用网格.放置、网格和包装都可以使用.只是有些更自然地适合特定类型的问题.在这种情况下,看起来您正在创建一个实际的网格——标签的行和列——所以网格是自然的选择.

You ask about why you must use grid. There is no requirement to use grid. Place, grid and pack can all be used. It's simply that some are more naturally suited to particular types of problems. In this case it looks like you're creating an actual grid -- rows and columns of labels -- so grid is the natural choice.

在画布上创建窗口时使用 anchor='nw' 有什么特别之处?"

"What so special about using anchor='nw' when creating window on canvas?"

锚点告诉您窗口的哪个部分位于您提供的坐标处.默认情况下,窗口的中心将放置在该坐标处.对于上面的代码,您希望左上角(西北")位于坐标处.

The anchor tells you what part of the window is positioned at the coordinates you give. By default, the center of the window will be placed at the coordinate. In the case of your code above, you want the upper left ("northwest") corner to be at the coordinate.

这篇关于框架的 Tkinter 滚动条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆