为什么我的Tkinter滚动条不起作用?

用户名

我的笔记本小部件中有3个标签,并且我想在第3个标签上有一个可滚动的框架。我设置了一个画布和一个滚动条,并设置了所有命令以便它们进行交互,但是它不起作用。我究竟做错了什么?完整的可运行代码如下:

import subprocess
from Tkinter import *
from ttk import *
import piper as Piper
import sqlite3



def confCanvas(event):
    global viewKeysCanvas
    print "ConfCanvasEvent\n";
    viewKeysCanvas.configure(scrollregion=viewKeysCanvas.bbox("all"))



root =Tk()

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

root.title('Scroll Test')

note = Notebook(root)

tab1 = Frame(note)
tab2 = Frame(note)
tab3 = Frame(note)

tab1.pack()
tab2.pack()
tab3.pack(fill=BOTH, expand=True)



#tab1
printGroup = Frame(tab1)
Label(printGroup, text="Test label1").pack()
printGroup.pack()

#tab2
bulkGroup = Frame(tab2)
Label(bulkGroup, text="Test label2").pack()
bulkGroup.pack()


#tab3
vkFrame = Frame(tab3)
viewKeysCanvas = Canvas(vkFrame)
viewKeysGroup = Frame(viewKeysCanvas)
viewKeysScrollbar = Scrollbar(vkFrame, orient="vertical", command=viewKeysCanvas.yview)
viewKeysCanvas.configure(yscrollcommand=viewKeysScrollbar.set)

viewKeysScrollbar.pack(side=RIGHT, fill=Y)
viewKeysCanvas.pack(fill="both", expand=True)
viewKeysCanvas.create_window((0,0), window=tab3)
vkFrame.bind("<Configure>",confCanvas)
vkFrame.pack()

for x in range(0, 9):
    aKeyGroup = LabelFrame(viewKeysGroup, text="number: "+str(x))
    buttonFrame = Frame(aKeyGroup)
    Button(buttonFrame, text="Action 1").pack(padx=10, side=LEFT)
    Button(buttonFrame, text="Action 2").pack(padx=10, side=LEFT)
    Label(aKeyGroup, text="Public key: ").pack(side=TOP)
    Label(aKeyGroup, text="Private key: ").pack(side=TOP)
    buttonFrame.pack(padx=10, pady=10)      
    aKeyGroup.pack()



viewKeysGroup.pack(padx=10, pady=10)


note.add(tab1, text = "Test tab 1")
note.add(tab2, text = "Test tab 2")
note.add(tab3, text = "Test tab 3")


note.pack(expand=True, fill=BOTH)


root.mainloop()

我正在使用LXDE在Debian上使用Python 2.7.3。我是一位经验丰富的程序员,但是我是Python的新手,所以如果我做的任何其他事情有误,请告诉我。谢谢你的帮助!

算了吧

为了使您的代码以最少的修改量工作,请进行更改

viewKeysCanvas.create_window((0,0), window=tab3)

viewKeysCanvas.create_window((0,0), window=viewKeysGroup)

并删除

# viewKeysGroup.pack(padx=10, pady=10)

我从本示例开始,然后对其进行修改以使其看起来更像您的代码,从而发现了这一点由于我逐步对其进行了修改,因此我始终可以使用可滚动的画布。当GUI看起来相同时,我只是比较了哪些行是不同的。下面是我最终得到的代码。

很抱歉,我无法解释为什么需要进行这些更改。我想改变tab3viewKeysGroup是合理的,但为什么你应该避免包装的viewKeysGroup我是无法理解。


import Tkinter as tk
import ttk

class Tab1(tk.Frame):
    def __init__(self, root):
        tk.Frame.__init__(self, root)
        tk.Label(self, text="Test label1").pack()

class Tab2(tk.Frame):
    def __init__(self, root):
        tk.Frame.__init__(self, root)
        tk.Label(self, text="Test label2").pack()

class Tab3(tk.Frame):
    def __init__(self, root):
        tk.Frame.__init__(self, root)
        self.viewKeysCanvas = tk.Canvas(self)
        self.viewKeysGroup = tk.Frame(self.viewKeysCanvas)
        self.viewKeysScrollbar = tk.Scrollbar(self, orient="vertical",
                                command=self.viewKeysCanvas.yview)
        self.viewKeysCanvas.configure(yscrollcommand=self.viewKeysScrollbar.set)
        self.viewKeysScrollbar.pack(side="right", fill="y")
        self.viewKeysCanvas.pack(fill="both", expand=True)
        self.viewKeysCanvas.create_window(0, 0, window=self.viewKeysGroup, anchor="nw")
        self.viewKeysGroup.bind("<Configure>", self.on_frame_configure)
        self.populate()

    def populate(self):
        for x in range(0, 9):
            aKeyGroup = tk.LabelFrame(self.viewKeysGroup, text="number: " + str(x))
            buttonFrame = tk.Frame(aKeyGroup)
            tk.Button(buttonFrame, text="Action 1").pack(padx=10, side="left")
            tk.Button(buttonFrame, text="Action 2").pack(padx=10, side="left")
            tk.Label(aKeyGroup, text="Public key: ").pack(side="top")
            tk.Label(aKeyGroup, text="Private key: ").pack(side="top")
            buttonFrame.pack(padx=10, pady=10)
            aKeyGroup.pack()

    def on_frame_configure(self, event):
        """Reset the scroll region to encompass the inner frame"""
        self.viewKeysCanvas.configure(scrollregion=self.viewKeysCanvas.bbox("all"))

root = tk.Tk()
sizex, sizey, posx, posy = 800, 500, 100, 100
root.wm_geometry("%dx%d+%d+%d" % (sizex, sizey, posx, posy))
root.title('Scroll Test')

note = ttk.Notebook(root)
tab1 = Tab1(note)
tab2 = Tab2(note)
tab3 = Tab3(note)

note.add(tab1, text="Test tab 1")
note.add(tab2, text="Test tab 2")
note.add(tab3, text="Test tab 3")

note.pack(expand=True, fill="both")
root.mainloop()

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

为什么我的滚动条未总是正确绘制?

来自分类Dev

高图滚动条不起作用

来自分类Dev

为什么我看不到滚动条的底部

来自分类Dev

当我在jscrollpane上添加jtable时,水平滚动条不起作用

来自分类Dev

滚动条在JPane内的JScrollPanel上不起作用

来自分类Dev

为什么在我的网站上显示水平滚动条?

来自分类Dev

CSS:为什么我的滚动条高度为100%?

来自分类Dev

完美的滚动条不起作用

来自分类Dev

为什么我的滚动条看起来很古怪?

来自分类Dev

为什么我的Tkinter滚动条位于应该滚动的区域的顶部?

来自分类Dev

为什么我的平滑滚动功能不起作用?

来自分类Dev

为什么我的tkinter水平滚动条在右下角被挤压,但垂直滚动条看起来正常?

来自分类Dev

为什么我的UIScrollView水平滚动条不显示?

来自分类Dev

scrollarea中的滚动条不起作用

来自分类Dev

为什么我的滚动窗格不起作用

来自分类Dev

水平滚动条不起作用asp.net

来自分类Dev

为什么用这个jQuery脚本替换滚动条上的徽标不起作用?

来自分类Dev

jQuery滚动条不起作用

来自分类Dev

滚动条在Java中不起作用

来自分类Dev

咒语滚动条不起作用

来自分类Dev

Python Tkinter滚动条不起作用

来自分类Dev

Tkinter滚动条不起作用

来自分类Dev

Python tkinter:画布滚动条出现但不起作用

来自分类Dev

从idiotWu反应光滑的滚动条不起作用?

来自分类Dev

为什么我的div滚动条不起作用?

来自分类Dev

tkinter:滚动条出现但不起作用

来自分类Dev

隐藏滚动条在 Mozilla Firefox 中不起作用

来自分类Dev

滚动条不起作用 - Selenium Webdriver

来自分类Dev

约束布局中的滚动条不起作用

Related 相关文章

热门标签

归档