我尝试使用https://stackoverflow.com/a/61588726/13512812来具有可滚动按钮,但是由于某些原因,滚动无法正常工作。同样,如果不清楚,我的目标是,当按下某个按钮时,会将按钮添加到可滚动框架中。 这就是我所做的
from tkinter import *
import tkinter as tk
class SeaofBTCapp(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
container = tk.Frame(self)
container.pack(side="top", fill="both", expand=True)
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)
self.frames = {}
for F in (StartPage,Task):
frame = F(container, self)
self.frames[F] = frame
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame(StartPage)
def show_frame(self, cont):
frame = self.frames[cont]
frame.tkraise()
frame.winfo_toplevel().geometry("1024x720")
frame.configure(bg='#333130')
mylist = ['item1','item2','item3','item4','item5','item6','item7','item8','item9']
class StartPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
framecon = Frame(self)
#framecon.configure(bg='red')
global canvascoon
canvascon = Canvas(framecon, height=100)
global frame2
frame2 = Frame(canvascon)
myscrollbar=Scrollbar(framecon,orient="vertical",command=canvascon.yview) # will be visible if the frame2 is to to big for the canvas
canvascon.create_window((0,0),window=frame2,anchor='nw')
Button(self,text='go',command=lambda: meth()).pack()
frame2.update() # update frame2 height so it's no longer 0 ( height is 0 when it has just been created )
canvascon.configure(yscrollcommand=myscrollbar.set, scrollregion="0 0 0 %s" % frame2.winfo_height())
canvascon.pack(side=LEFT)
myscrollbar.pack(side=RIGHT, fill = Y)
framecon.pack()
def meth():
for item in mylist:
c = Canvas(frame2, height=50, width=100, bg="red")
lab = tk.Label(frame2, text='Item')
lab_window = c.create_window(50, 12.5, window=lab)
c.pack()
class Task(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
app = SeaofBTCapp()
app.mainloop()
非常感谢您的帮助。
答案 0 :(得分:0)
现在在这里工作:)
from tkinter import *
import tkinter as tk
class SeaofBTCapp(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
container = tk.Frame(self)
container.pack(side="top", fill="both", expand=True)
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)
self.frames = {}
for F in (StartPage,Task):
frame = F(container, self)
self.frames[F] = frame
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame(StartPage)
def show_frame(self, cont):
frame = self.frames[cont]
frame.tkraise()
frame.winfo_toplevel().geometry("1024x720")
frame.configure(bg='#333130')
mylist = ['item1','item2','item3','item4','item5','item6','item7','item8','item9']
class StartPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
framecon = Frame(self)
#framecon.configure(bg='red')
global canvascon
canvascon = Canvas(framecon, height=100)
global frame2
frame2 = Frame(canvascon)
global myscrollbar
myscrollbar=Scrollbar(framecon,orient="vertical",command=canvascon.yview) # will be visible if the frame2 is to to big for the canvas
canvascon.create_window((0,0),window=frame2,anchor='nw')
Button(self,text='go',command=lambda: meth()).pack()
canvascon.pack(side=LEFT)
myscrollbar.pack(side=RIGHT, fill = Y)
framecon.pack()
def meth():
for item in mylist:
c = Canvas(frame2, height=50, width=100, bg="red")
lab = tk.Label(frame2, text='Item')
lab_window = c.create_window(50, 12.5, window=lab)
c.pack()
frame2.update() # update frame2 height so it's no longer 0 ( height is 0 when it has just been created )
canvascon.configure(yscrollcommand=myscrollbar.set, scrollregion="0 0 0 %s" % frame2.winfo_height())
class Task(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
app = SeaofBTCapp()
app.mainloop()