簡體   English   中英

如何使用 tkinter 在可滾動框架內存儲和添加水平框架?

[英]How to store and add horizontal frames within a scrollable frame using tkinter?

我的目標是設計

我正在嘗試實現這樣的設計,您在一個可滾動的框架內有一個 canvas,所以我可以在框架中有很多不同的條目。 我已經添加了我目前擁有的代碼。 列表框只是一個測試,我不會使用列表框。 我基本上需要一種在我認為的滾動條框架內存儲大量水平框架的方法,它可以容納 canvas 來顯示圖像和一些按鈕/標簽。 我目前擁有的代碼意味着可滾動的框架 window 非常小,所以我需要知道如何放大它以及如何制作它,以便我可以在可滾動的框架中存儲盡可能多的框架。 謝謝

    customisationWindow = tk.Toplevel(mainWindow)
    customisationWindow.geometry("700x580")
    customisationWindow.title("DIY Store Quote System")
    tabControl = ttk.Notebook(customisationWindow)

    tab1 = ttk.Frame(tabControl, width=300)
    tab1.pack(fill="both", expand=1)
    tab2 = ttk.Frame(tabControl)
    tab3 = ttk.Frame(tabControl)
    tab4 = ttk.Frame(tabControl)
    tab5 = ttk.Frame(tabControl)

    tabControl.add(tab1, text='Select Wallpaper')
    tabControl.add(tab2, text='Select Colour')
    tabControl.add(tab3, text='Select Detailing')
    tabControl.add(tab4, text='Add Adhesive/Lining')
    tabControl.add(tab5, text='View Basket')
    tabControl.pack(expand=1, fill="both")
    createTab1(tab1)
    createTab2(tab2)
    createTab3(tab3)
    createTab4(tab4)
    createTab5(tab5)


def createTab5(tab):

    yourBasketLabel = Label(tab, text="Your Basket", bg="#94B3BD",
                        font=("Times New Roman", 14)).place(x=50, y=50)

    basketFrame = Frame(tab, width=550, height=400)
    basketFrame.place(x=50, y=70)
    basketCanvas = Canvas(basketFrame, width=550, height=400)
    basketCanvas.place(x=50, y=70)
    scrollbar = Scrollbar(basketCanvas)
    scrollbar.pack(side=RIGHT, fill=Y)
    
    mylist = Listbox(basketFrame, yscrollcommand=scrollbar.set)
    for line in range(100):
        mylist.insert(END, "This is line number " + str(line))

    mylist.pack(side=LEFT, fill=BOTH)
    scrollbar.config(command=mylist.yview)


mainWindow = Tk()
mainFrame = Frame(mainWindow, bg="#94B3BD")
mainFrame.pack(fill="both", expand=True)
verticalFrame = Frame(mainFrame, bg="#94B3BD")

mainWindow.geometry("500x200")

welcomeMessage = tk.Label(verticalFrame, text="Welcome to the DIY Store Quote System", background="#94B3BD",
                          pady=40, font=("Arial", 25)).pack()
startButton = tk.Button(verticalFrame, highlightbackground='#B1E4EC', text="Begin Customising Wallpaper",
                        font=("Arial", 25),
                        pady=20, command=open).pack()
verticalFrame.pack()

mainWindow.mainloop()

這段代碼應該可以解決問題:

import tkinter as tk

def addFrame(window, frames):
    frame = tk.Frame(window, width=50, height=50)
    label = tk.Label(frame, text = "New frame number {} wow!".format(len(frames)))
    label.pack()
    frame.pack()

    frames.append(frame)
    print("Those are all the frames:", frames)

my_frames = []

window = tk.Tk()
button = tk.Button(window, text = 'Add frame', command = lambda: addFrame(window, my_frames)) 
button.pack()
window.mainloop()
  • 每次按下按鈕時,都會使用其獨特的 label 創建新框架。
  • 所有框架都存儲在my_frames列表中供您訪問。

您可以將滾動條添加到 canvas,然后使用 create_window 將框架添加到create_window中。 然后,您可以向myList添加更多幀。

def createTab5(tab):

    yourBasketLabel = Label(tab, text="Your Basket", bg="#94B3BD",
                        font=("Times New Roman", 14)).place(x=50, y=50)

    basketFrame = Frame(tab, width=550, height=400)
    basketFrame.place(x=50, y=70)
    basketCanvas = Canvas(basketFrame, width=550, height=400)
    basketCanvas.pack(fill = "both", expand = True, side = "left")
    scrollbar = Scrollbar(basketFrame, command = basketCanvas.yview)
    scrollbar.pack(side=RIGHT, fill=Y)
    basketCanvas.config(yscrollcommand = scrollbar.set)
    basketCanvas.bind("<Configure>", resizeCanvas)
    myList = tk.Frame(basketCanvas)
    basketCanvas.create_window((4,4), window = myList, anchor = "nw")
    for line in range(100):
        frame = tk.Frame(myList)
        frame.pack()
        label = tk.Label(frame, text = "This is line number {}".format(str(line)))
        label.pack()
    scrollbar.config(command=basketCanvas.yview)

def resizeCanvas(event):
    canvas = event.widget
    canvas.update_idletasks()
    canvas.config(scrollregion = canvas.bbox("all"))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM