整个窗口的Python Tkinter滚动条

ilmyapht  于 2023-01-08  发布在  Python
关注(0)|答案(7)|浏览(267)

有没有一种方法可以将滚动条添加到整个窗口中,而不必将所有内容都放入框架中?我已经使用.grid设置了所有内容,我不喜欢将所有内容都放入框架中的想法。

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

circus()#calls the function to set up everything

root.mainloop()
bvhaajcl

bvhaajcl1#

如何在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()
z8dt9xmd

z8dt9xmd2#

你也许能设置一个滚动条到根.

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)

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

hiz5n14c

hiz5n14c3#

这种方法不使用Frame对象,不同之处在于它使用Scrollbars创建了一个非常大的Canvas,并要求您在上面显示图像。
然后将屏幕设置为self.root.wm_attributes("-fullscreen", 1)self.root.wm_attributes("-top", 1)
按Esc键或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()

我以前的答案远远超出了问题的要求,所以这是一个削减版本更准确地回答了这个问题。

nr9pn0ug

nr9pn0ug4#

来自伟大的effbot docs
在Tkinter中,滚动条是一个独立的小部件,可以附加到任何支持标准滚动条界面的小部件上。这些小部件包括:

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

所以,你不能直接在Frame中使用滚动条,你可以创建自己的Frame子类来支持滚动条接口。
在上面列出的4个小部件中,只有一个小部件允许在其中使用其他小部件:画布。您可以使用画布来拥有可滚动的内容,但是将小部件放置在画布中不使用 Package 或网格,而是使用显式的像素位置(即在画布上绘制)。

fnx2tebb

fnx2tebb5#

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

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
oo7oh9g9

oo7oh9g96#

我确实尝试了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()
e5nqia27

e5nqia277#

我应用codecAkash Shendage到我的项目,但它不工作。我有100s标签和这些标签我使用的地方方法这是由于地方方法,而不是网格

相关问题