整个窗口的Python Tkinter Scrollbar

时间:2013-11-08 13:17:48

标签: python tkinter

有没有办法在不将所有内容放入框架的情况下向我的整个窗口添加滚动条?我用.grid设置了一切,我不喜欢围绕一切包装框架的想法。

root = Tk()
root.maxsize(900,600)

circus()#calls the function to set up everything

root.mainloop()

5 个答案:

答案 0 :(得分:0)

您可以将滚动条设置为root。

scrollderoot = tkinter.Scrollbar(orient="vertical", command=root.yview)
scrollderoot.grid(column=5, row=0, sticky='ns', in_=root) #instead of number 5, set the column as the expected one for the scrollbar. Sticky ns will might be neccesary.
root.configure(yscrollcommand=scrollderoot.set)

老实说,我没有尝试过,但“应该”有效。祝你好运。

答案 1 :(得分:0)

来自伟大的effbot docs

  

在Tkinter中,滚动条是一个可以附加到的单独小部件   任何支持标准滚动条界面的小部件。这样的小部件   包括:

     
      
  • 列表框小部件。
  •   
  • 文本小部件。
  •   
  • Canvas小部件
  •   
  • Entry小部件
  •   

因此,您无法直接在Frame中使用滚动条。可以创建自己的支持滚动条界面的Frame子类。

在上面列出的4个小部件中,只有一个允许其中的其他小部件:Canvas。您可以使用Canvas具有可滚动内容,但在Canvas中放置小部件不使用包或网格,而是使用显式像素位置(即在Canvas上绘画)。

答案 2 :(得分:0)

如何在tkinter中将滚动条添加到全窗口?

这是python 3的答案...


from tkinter import *

from tkinter import ttk



root = Tk()

root.title('Full Window Scrolling X Y Scrollbar Example')

root.geometry("1350x400")



# Create A Main frame

main_frame = Frame(root)

main_frame.pack(fill=BOTH,expand=1)



# Create Frame for X Scrollbar

sec = Frame(main_frame)

sec.pack(fill=X,side=BOTTOM)



# Create A Canvas

my_canvas = Canvas(main_frame)

my_canvas.pack(side=LEFT,fill=BOTH,expand=1)



# Add A Scrollbars to Canvas

x_scrollbar = ttk.Scrollbar(sec,orient=HORIZONTAL,command=my_canvas.xview)

x_scrollbar.pack(side=BOTTOM,fill=X)

y_scrollbar = ttk.Scrollbar(main_frame,orient=VERTICAL,command=my_canvas.yview)
y_scrollbar.pack(side=RIGHT,fill=Y)



# Configure the canvas

my_canvas.configure(xscrollcommand=x_scrollbar.set)

my_canvas.configure(yscrollcommand=y_scrollbar.set)

my_canvas.bind("<Configure>",lambda e: my_canvas.config(scrollregion= my_canvas.bbox(ALL))) 



# Create Another Frame INSIDE the Canvas

second_frame = Frame(my_canvas)



# Add that New Frame a Window In The Canvas

my_canvas.create_window((0,0),window= second_frame, anchor="nw")





for thing in range(100):

    Button(second_frame ,text=f"Button  {thing}").grid(row=5,column=thing,pady=10,padx=10)



for thing in range(100):

    Button(second_frame ,text=f"Button  {thing}").grid(row=thing,column=5,pady=10,padx=10)



root.mainloop()

答案 3 :(得分:0)

这是一个类和一些示例用法,它使用 .place 方法为整个窗口添加滚动条。您可以创建一个 Frame 对象,并将其放置在所需的 (x, y) 坐标处。然后,只需在 Frame 中传递您的 root 对象代替 main.frame,以在您想要的坐标处创建一个可滚动窗口。

from tkinter import *

class ScrollableFrame:
    """A scrollable tkinter frame that will fill the whole window"""

    def __init__ (self, master, width, height, mousescroll=0):
        self.mousescroll = mousescroll
        self.master = master
        self.height = height
        self.width = width
        self.main_frame = Frame(self.master)
        self.main_frame.pack(fill=BOTH, expand=1)

        self.scrollbar = Scrollbar(self.main_frame, orient=VERTICAL)
        self.scrollbar.pack(side=RIGHT, fill=Y)

        self.canvas = Canvas(self.main_frame, yscrollcommand=self.scrollbar.set)
        self.canvas.pack(expand=True, fill=BOTH)

        self.scrollbar.config(command=self.canvas.yview)

        self.canvas.bind(
            '<Configure>',
            lambda e: self.canvas.configure(scrollregion=self.canvas.bbox("all"))
        )

        self.frame = Frame(self.canvas, width=self.width, height=self.height)
        self.frame.pack(expand=True, fill=BOTH)
        self.canvas.create_window((0,0), window=self.frame, anchor="nw")

        self.frame.bind("<Enter>", self.entered)
        self.frame.bind("<Leave>", self.left)

    def _on_mouse_wheel(self,event):
        self.canvas.yview_scroll(-1 * int((event.delta / 120)), "units")

    def entered(self,event):
        if self.mousescroll:
            self.canvas.bind_all("<MouseWheel>", self._on_mouse_wheel)
        
    def left(self,event):
        if self.mousescroll:
            self.canvas.unbind_all("<MouseWheel>")

# EXAMPLE USAGE

obj = ScrollableFrame(
    master,
    height=300, # Total required height of canvas
    width=400 # Total width of master
)

objframe = obj.frame
# use objframe as the main window to make widget

答案 4 :(得分:0)

这种方法不使用 Frame 对象,不同之处在于它创建了一个非常大的 CanvasScrollbars,并要求您在其上显示图像。

然后将屏幕设置为 self.root.wm_attributes("-fullscreen", 1)self.root.wm_attributes("-top", 1)

按 Escape 键或 Alt-F4 关闭。

import tkinter as tk
from tkinter import filedialog as fido

class BigScreen:

    def __init__( self ):
        self.root = tk.Tk()
        self.root.rowconfigure(0, weight = 1)
        self.root.columnconfigure(0, weight = 1)

        w, h = self.root.winfo_screenwidth(), self.root.winfo_screenheight()
        self.canvas = tk.Canvas(self.root, scrollregion = f"0 0 {w*2} {h*2}")
        self.canvas.grid(row = 0, column = 0, sticky = tk.NSEW)
        self.makescroll(self.root, self.canvas )

        self.imagename = fido.askopenfilename( title = "Pick Image to View" )
        if self.imagename:
            self.photo = tk.PhotoImage(file = self.imagename).zoom(2, 2)
            self.window = self.canvas.create_image(
                ( 0, 0 ), anchor = tk.NW, image = self.photo)

        self.root.bind("<Escape>", self.closer)
        self.root.wm_attributes("-fullscreen", 1)
        self.root.wm_attributes("-top", 1)

    def makescroll(self, parent, thing):
        v = tk.Scrollbar(parent, orient = tk.VERTICAL, command = thing.yview)
        v.grid(row = 0, column = 1, sticky = tk.NS)
        thing.config(yscrollcommand = v.set)
        h = tk.Scrollbar(parent, orient = tk.HORIZONTAL, command = thing.xview)
        h.grid(row = 1, column = 0, sticky = tk.EW)
        thing.config(xscrollcommand = h.set)

    def closer(self, ev):
        self.root.destroy()

if __name__ == "__main__":
    Big = BigScreen()
    Big.root.mainloop()

我之前的回答远远超出了所提出的问题,因此这是一个精简版,可以更准确地回答问题。