1

有没有办法在我的整个窗口中添加滚动条而不将所有内容都放入框架中?我已经用 .grid 设置了所有内容,并且我不喜欢在所有内容周围包裹框架的想法。

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

circus()#calls the function to set up everything

root.mainloop()
4

6 回答 6

1

如何在 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()
于 2020-11-18T10:04:47.100 回答
1

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

然后用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()

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

于 2021-08-08T15:48:10.293 回答
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)

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

于 2013-11-08T14:36:58.437 回答
0

来自伟大的effbot 文档

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

  • 列表框小部件。
  • 文本小部件。
  • 画布小部件
  • 条目小部件

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

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

于 2013-11-08T15:24:30.290 回答
0

这是一个类和一些示例用法,它使用该.place方法为整个窗口添加滚动条。您可以创建一个Frame对象,并将其放置在所需的 (x, y) 坐标处。然后,只需传递您的Frame对象代替rootinmain.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
于 2021-08-08T12:16:30.393 回答
0

我确实尝试了 Akash Shendage 的答案,这对我来说开箱即用。但是经过一些调整,它就可以工作了。

#!/bin/env python3

from tkinter import ttk
import tkinter as tk

root = tk.Tk()
root.title('Full Window Scrolling X Y Scrollbar Example')
root.geometry("1350x400")

# Create A Main frame
main_frame = tk.Frame(root)
main_frame.pack(fill=tk.BOTH,expand=1)

# Create Frame for X Scrollbar
sec = tk.Frame(main_frame)
sec.pack(fill=tk.X,side=tk.BOTTOM)

# Create A Canvas
my_canvas = tk.Canvas(main_frame)
my_canvas.pack(side=tk.LEFT,fill=tk.BOTH,expand=1)

# Add A Scrollbars to Canvas
x_scrollbar = ttk.Scrollbar(sec,orient=tk.HORIZONTAL,command=my_canvas.xview)
x_scrollbar.pack(side=tk.BOTTOM,fill=tk.X)
y_scrollbar = ttk.Scrollbar(main_frame,orient=tk.VERTICAL,command=my_canvas.yview)
y_scrollbar.pack(side=tk.RIGHT,fill=tk.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(tk.ALL))) 

# Create Another Frame INSIDE the Canvas
second_frame = tk.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):
    tk.Button(second_frame ,text=f"Button  {thing}").grid(row=5,column=thing,pady=10,padx=10)

for thing in range(100):
    tk.Button(second_frame ,text=f"Button  {thing}").grid(row=thing,column=5,pady=10,padx=10)

root.mainloop()
于 2021-12-08T19:00:12.683 回答