滚动条不滚动画布内部框架内的小部件

时间:2019-07-10 19:31:41

标签: python-3.x tkinter

我在Tk上有画布和滚动条。 我在画布上有一个框架。 我将带有小部件的新框架添加到该框架中,并希望滚动这些小部件。 滚动条根本不滚动小部件,并且当我添加窗口下方的小部件时,滚动条变成灰色,我根本无法使用它。

我是tkinter和python的新手。我只是不知道我在做什么。我没有尝试过上课(应该吗?)。我尝试使用ttk,并在网站周围寻找了非课堂答案,但没有一个起作用。

from tkinter import *
from tkinter.ttk import *
actor_number=0
global tk 





def new_actor_button_command():
  global menu_frame
  global actor_number
  global canvas

  new_actor_frame=Frame(menu_frame,width=500,height=200)
  new_actor_frame.grid(row=1+actor_number,column=0,pady=20)
  actor_name_label=Label(new_actor_frame,text="Actor Name")
  new_actor_frame.place(relx=0.0, rely=0.0, anchor=CENTER)

  delete_actor_button=Button(new_actor_frame,text="Delete
                             Actor",command=delete_actor_button_command)
  new_actor_frame.grid(row=1+actor_number,column=1)

  actor_name_label.grid(row=2+actor_number,column=1)
  delete_actor_button.grid(row=2+actor_number,column=2)
  actor_number+=1
  canvas.update_idletasks()

  scrollbar.config(command=canvas.yview)
  canvas.configure(scrollregion=canvas.bbox("all"))



def make_new_actor():
  global canvas
  global menu_frame
  new_actor_button=Button(menu_frame,text="Add New
                          Actor",command=new_actor_button_command)
  new_actor_button.grid(row=0,column=0)


def new_command():
  global actor_number
  actor_number=0
  make_new_actor()
tk=Tk()

tk.geometry("1200x800")
menubar=Menu(tk)

filemenu=Menu(menubar,tearoff=0)
filemenu.config(font=("Verdana",16))
filemenu.add_command(label="New",font=("Verdana",16),command=new_command)
menubar.add_cascade(label="File", menu=filemenu)

global scrollbar
canvas=Canvas(tk,width=1000,height=1000)
scrollbar=Scrollbar(tk,orient="vertical",command = canvas.yview)

menu_frame=Frame(canvas,width=1000,height=1000)
canvas.create_window(0,0,window=menu_frame)

canvas.configure(yscrollcommand=scrollbar.set)
canvas.configure(scrollregion=canvas.bbox("all"))
canvas.place(relx=0.0, rely=0.0)
menu_frame.pack(side=LEFT,expand=True)
scrollbar.pack(side=RIGHT,fill=Y)
tk.config(menu=menubar)
tk.mainloop()

它应该滚动“ menu_frame”内的vidgets。

1 个答案:

答案 0 :(得分:0)

滚动画布并不容易。

它滚动项目(如果您在menu_frame中添加了足够的项目),但是可能需要在new_actor_frame中进行其他更改

from tkinter import *
from tkinter.ttk import *

def new_actor_button_command():
    new_actor_frame = Frame(menu_frame, width=500, height=200)
    new_actor_frame.grid(row=actor_number, column=0)

    actor_name_label = Label(new_actor_frame, text="Actor Name")
    actor_name_label.grid(row=0, column=1)

    delete_actor_button=Button(new_actor_frame,text="Delete Actor")#, command=delete_actor_button_command)
    delete_actor_button.grid(row=0, column=2)

def new_command():
    global actor_number

    actor_number += 1

    new_actor_button = Button(menu_frame, text="Add New Actor", command=new_actor_button_command)
    new_actor_button.grid(row=actor_number, column=0)

def update_canvas(event=None):
    canvas.configure(scrollregion=canvas.bbox("all"))

actor_number=0

tk = Tk()
tk.geometry("1200x800")

menubar = Menu(tk)
filemenu = Menu(menubar, tearoff=0)
filemenu.add_command(label="New", command=new_command)
menubar.add_cascade(label="File", menu=filemenu)
tk.config(menu=menubar)

canvas = Canvas(tk, background='white')#, width=1000, height=1000)
canvas.pack(side='left', fill='both', expand=True)

scrollbar = Scrollbar(tk, orient="vertical", command=canvas.yview)
scrollbar.pack(side='right', fill='y')

menu_frame = Frame(canvas)
canvas.create_window(0, 0, window=menu_frame, anchor='nw')

canvas.configure(yscrollcommand=scrollbar.set)
canvas.bind('<Configure>', update_canvas) # update when change size

tk.mainloop()