简体   繁体   English

滚动条在 tkinter 应用程序中变灰

[英]Scroll bar greyed out in tkinter application

I am trying to add a scroll bar into my tkinter application.我正在尝试将滚动条添加到我的 tkinter 应用程序中。 It is appearing, however it is greyed out and is not able to scroll and I am not sure why despite the fact that I have many widgets beyond the height of the page.它正在出现,但它是灰色的并且无法滚动,尽管我有许多超出页面高度的小部件,但我不确定为什么。 Any help would be appreciated.任何帮助,将不胜感激。

import calendar
import tkinter as tk
from tkinter import ttk


root = tk.Tk()
root.geometry('800x800')


x = 30
y = 30
box_image = tk.PhotoImage(file='apple.png')



### SCROLL BAR ###

# create a main frame
main_frame = tk.Frame(root)
main_frame.pack(fill='both', expand=1)

# create a canvas
my_canvas = tk.Canvas(main_frame)
my_canvas.pack(side='left', fill='both', expand=1)

# add a scrollbar to the canvas
my_scrollbar = ttk.Scrollbar(main_frame, orient='vertical', command=my_canvas.yview)
my_scrollbar.pack(side='right', fill='y')

# configure the canvas
my_canvas.configure(yscrollcommand=my_scrollbar.set)
my_canvas.bind('<Configure>', lambda e: my_canvas.configure(scrollregion = my_canvas.bbox("all")))

# create Another frame inside the canvas
second_frame = tk.Frame(my_canvas)

# add that new frame to a window in the canvas
my_canvas.create_window((0,0), window=second_frame, anchor="nw")

def display():
    global x , y
    panel2 = tk.Label(main_frame, image=box_image, bg='#f7f6f6')
    panel2.place(x=x, y=y) 
    x = x
    y = y+200


button = tk.Button(main_frame, text="click me", command=display)
button.place(x=0, y=0)

for thing in range (100):
    tk.Button(main_frame, text=f'Button {thing}Yo!').pack()

root.mainloop()

For the frame-in-a-canvas trick to work, you must add the buttons to the inner frame:要使画布中的框架技巧起作用,您必须将按钮添加到内部框架中:

for thing in range (100):
    tk.Button(second_frame, text=f'Button {thing}Yo!').pack()
    #         ^^^^^^^^^^^^

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM