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

查看:305
本文介绍了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. 为什么必须使用网格方法? (我尝试了放置方法,但是所有标签都没有出现在画布上.)
  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.

推荐答案

我做对了吗?是否有更好/更智能的方法来实现此代码给我的输出?

通常来说,是的,您做对了.除了画布,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.

为什么必须使用网格方法?(我尝试过放置方法,但是画布上没有标签吗?)

您询问为什么必须使用网格.不需要使用网格.放置,网格和包装都可以使用.只是有些更自然地适合特定类型的问题.在这种情况下,您似乎正在创建一个实际的网格-标签的行和列-因此网格是自然的选择.

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'有何特别之处?

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

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天全站免登陆