如何使用包装和放置使我的 tkinter 框架具有正确的尺寸?

问题描述

我正在尝试在可滚动框架内创建大量框架。当按下按钮(位于添加到购物篮区域方法中)时,您应该能够向可滚动框架添加一个框架。目前我的代码看起来很奇怪,按两次按钮后,没有任何明显的区别。如果我在添加到购物篮方法中执行 itempreview.place(x=10,y=10) 而不是 .pack() 。它看起来像我想要的......所以框架的宽度是可滚动框架的长度。并且高度大约是 150?。但是,这意味着单击按钮时您看不到任何其他帧。 (它们是否都被放置在彼此之上?)我想知道如何制作它以便在我使用 itempreview.place(x=10,y=10) 时可以保持帧的外观,但确保添加下一帧会放在下面吗?

抱歉代码有点长,我尝试添加您需要的所有内容来重现它。

currentWallpaper = {}
wallpaperList =[]
framesForItemsInBasket = []
myList = tk.Frame
addToFrame = tk.Frame

def saveWallPaper():

    wallpaperList.append(currentWallpaper)
    addToBasket()
    setWallpaperToDefault()


def addBasketArea(tab):
    infoLabel = Label(tab,text="When you are happy with your design \n please enter how many meters you require \n "
                                "and add it to the basket.",bg="#94B3BD").place(x=400,y=390)
    addToBasket = Button(tab,highlightbackground='#B1E4EC',text="Add to Basket",font=("Arial",20),command=saveWallPaper)
    addToBasket.place(x=550,y=510)
    metersMessage = Label(tab,text = "Enter amount of meters",bg="#94B3BD",14)).place(x=380,y=480)
    metersBox = tk.Entry(tab,textvariable=metersNeeded,validate="key",validatecommand=callBack,bg="#94B3BD").place(x=380,y=510,width=160)
    # tab.overrideredirect(True)
    price = Label(tab,textvariable = currentPrice,bg="#94B3BD"). place(x=610,y=480)
    priceheading = Label(tab,text = "Price:",14)).place(x=550,y=480)


def open():

    style = ttk.Style()
    style.theme_create("yummy",parent="alt",settings={
        "TFrame": {"configure": {"background": '#94B3BD'}},"TNotebook": {
            "configure": {"background": "#5D7881"}},"TNotebook.Tab": {
            "configure": {"padding": [10,4],"background": "#94B3BD"}}})

    style.theme_use("yummy")
    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)
    tabList = [tab1,tab2,tab3,tab4,tab5]


def createTab5(tab):

    global addToFrame
    yourBasketLabel = Label(tab,text="Your Basket",font=("Times New Roman",14)).place(x=50,y=50)

    basketFrame = Frame(tab,width=550,height=350)
    basketFrame.place(x=50,y=70)
    basketCanvas = Canvas(basketFrame,highlightbackground = "#94B3BD",highlightcolor= "#94B3BD",height=350)
    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,bg="#94B3BD")

    basketCanvas.create_window((4,4),window=myList,anchor="nw",height= 200,width = 500)
    addToFrame= myList

    scrollbar.config(command=basketCanvas.yview)
    addBasketArea(tab)


def resizeCanvas(event):

    canvas = event.widget
    canvas.update_idletasks()
    canvas.config(scrollregion=canvas.bBox("all"))


def addToBasket():

    frame = tk.Frame(addToFrame,background = "blue",width = 550,height=400)
    frame.pack()

    label = tk.Label(frame,background= "yellow",text="New item!!".format(len(framesForItemsInBasket)))
    label.place(x=10,y=80)
    itemPreview = Canvas(frame,width = 120,height = 120,highlightthickness=0)
    itemPreview.pack()
 

mainWindow = Tk()
summary = StringVar()
tabList = []

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

mainWindow.geometry("500x200")

startButton = tk.Button(verticalFrame,text="Begin Customising Wallpaper",25),pady=20,command=open).pack()
verticalFrame.pack()
mainWindow.mainloop()

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)