tkinter:无法使框架可滚动 [英] tkinter: Can't make frame scrollable

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

问题描述

在 python tkinter 上,我在 Toplevel 窗口上使用了 2 个不同的框架,一个在右侧,另一个在左侧.右侧的框架不可滚动.我在该框架的框架顶部创建了一个画布,并在该画布顶部创建了一个框架.我已使该画布可滚动并将小部件粘贴到该画布上,但它不可滚动.我附上了可滚动部分的代码.

On python tkinter, I am using 2 different frames on a Toplevel window, one on the right and another on the left. The frame which is on right side is not scrollable. I have created a canvas on top of the frame on that frame and one more frame on top of that canvas. I have made that canvas scrollable and pasted the widgets on that canvas but it's not scrollable. I am attaching the code of the scrollable part.

w1 = Canvas(frame2, width=600, height=300,background="white", scrollregion=(1500,1500,3000,3000))
scr_h1 = ttk.Scrollbar(frame2,orient=HORIZONTAL)
scr_h1.pack(side=BOTTOM,fill=X)
scr_h1.config(command=w1.xview)

scr_v1 = ttk.Scrollbar(frame2,orient=VERTICAL)
scr_v1.pack(side=RIGHT,fill=Y)
scr_v1.config(command=w1.yview)

w1.config(xscrollcommand=scr_h1.set,yscrollcommand=scr_v1.set)
w1.pack(fill=BOTH,expand=True)

推荐答案

此代码适用于我运行 Python 3.4 - tkinter 窗口弹出一个红色椭圆(用于测试),滚动条允许您导航框架.如果您使用的是 Python 2,请将 tkinter 更改为 Tkinter(大写 T).

This code works for me running Python 3.4 - a tkinter window pops up with a red oval (for testing), and the scrollbar allows you to navigate the frame. If you are using Python 2, change tkinter to Tkinter (capital T).

from tkinter import *

root = Tk()

frame2 = Frame(root)
frame2.pack(side=RIGHT)

w1 = Canvas(frame2, width=600, height=300,background="white", scrollregion=(0,0,3000,3000))


scr_h1 = Scrollbar(frame2,orient=HORIZONTAL)
scr_h1.pack(side=BOTTOM,fill=X)
scr_h1.config(command=w1.xview)

scr_v1 = Scrollbar(frame2,orient=VERTICAL)
scr_v1.pack(side=RIGHT,fill=Y)
scr_v1.config(command=w1.yview)

w1.config(xscrollcommand=scr_h1.set,yscrollcommand=scr_v1.set)
w1.pack(fill=BOTH,expand=True)

# inserted to see if it's actually scrolling
w1.create_oval(0,0,50,50,fill='red')


root.mainloop()

两个可能的问题

  1. 你为什么使用 ttk ScrollBar?简单的 tkinter 滚动条足以满足您的代码需求.如果出现问题,返回到更简单的模型可能会有所帮助.
  2. 为什么您的起始滚动区域是 1500 - 在任一方向的前 1500 个单位中放置在画布上的任何对象都不可见,使用此设置,这可能会给您一种滚动条不起作用的错觉.参见 http://effbot.org/zone/tkinter-scrollbar-patterns.htm 有关使用滚动条的更多信息.
  1. Why were you using a ttk ScrollBar? The simple tkinter scroll bar will suffice for your code. When things aren't working, it might help to go back to the simpler model.
  2. Why your starting scroll region was 1500 - any object placed on the canvas in the first 1500 units in either direction were not visible, with this setting, which may have given you the illusion that the scrollbar was not working. See http://effbot.org/zone/tkinter-scrollbar-patterns.htm for more information on using scroll bars.

有两个框架或它们的方向应该无关紧要,但如果您尝试混合管理器(网格、包装等),您可能会遇到问题.这些问题更像是停滞的程序,而不是静止的滚动条.

It should not matter that there are two frames or their orientation, though you may run into problems if you try to mix managers (grid,pack,etc.). These problems are more along the lines of stalled programs, not stationary scrollbars.

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

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