【问题标题】:Putting Button in Frame issue - tKinter将按钮放入框架问题 - tKinter
【发布时间】:2021-06-15 23:13:40
【问题描述】:

我最近开始在学校的编程项目中使用 tkinter,但在如何在我的程序中插入/格式化按钮时遇到了一些问题。 我一直在慢慢地尝试构建我的程序,从主布局开始,然后插入功能,但目前,我非常坚持在我创建的框架中插入按钮,我无法制作因为它而取得任何进展。

这是我开始创建主窗口的代码及其产生的结果。

from tkinter import *
from tkinter import Tk, StringVar, ttk


root = Tk()
root.geometry("1350x700+0+0")  #creating main window for program
root.title("Point of Sale System")


TabFrame = Frame(root, width=250, height=100, bd=3, relief="groove") #creating box for editor & till tabs
TabFrame.grid(row=0, column=0)

def editorClick (): #blank function for editor & till buttons
    return
def tillClick ():
    return

editorTab = Button(TabFrame, font=("arial", 25), text="Editor", command=editorClick) #creating buttons on screen
editorTab.grid(row=0, column=0)
tillTab = Button(TabFrame, font=("arial", 25), text="Till", command=tillClick)
tillTab.grid(row=0, column=1)



def clearClick(): #blank function for clear button
    return

ClearFrame = Frame(root, width=250, height=100, bd=3, relief="groove") #creating box for editor & till tabs
ClearFrame.grid(row=0, column=4)

clearButton = Button(ClearFrame, font=("arial", 25), text="Clear", command=clearClick)
clearButton.grid(row=0, column=4)


itemFrame = Frame(root, width=700, height=600, bd=5, relief="ridge") #creating basic boxes for item, receipt and total
itemFrame.grid(row=1, column=0, rowspan=2, columnspan=3)

receiptFrame = Frame(root, width=600, height=400, bd=5, relief="ridge")
receiptFrame.grid(row=1, column=3, columnspan=2)

totalFrame = Frame(root, width=600, height=200, bd=5, relief="ridge")
totalFrame.grid(row=2, column=3, columnspan=2)


root.mainloop()

但是,一旦我尝试在最后添加此代码(当然是在 root.mainloop() 之前):

def AddButton():
global item_num #calls global variable
item_num += 1
item_text = "Item"+str(item_num) #concatonates text & variable
item1.config(text=item_text) #updates label text - doesn't add multiple 
item1.pack()

addButton = Button(itemFrame, text="Add Button", padx=25, pady=25, command=AddButton)
addButton.pack()

窗口的大小完全改变以适合按钮周围,而不是保持相同的大小,按钮只是像这样在里面:

这就是我想要的结果,虽然我将如何将它们放置在框架中是另一个问题,但我只是想专注于简单地按钮框架中的功能按钮.

有人可以解释一下我必须做什么才能将按钮放在框架中而不是改变框架以适应它吗?我不确定它是否与我打包它的方式有关(尽管我尝试使用 .grid 导致什么都没有出现)?所以我真的很感激一些帮助:)

(尽管我知道我是否需要为此提出一个单独的问题,但也将不胜感激任何有助于定位按钮而不是简单地添加类似 15 行和列的方式的任何指导 :)))

【问题讨论】:

  • itemFrame.grid(row=1, column=0, rowspan=2, columnspan=3, sticky='nsew')addButtonFrame.pack( expand=1, fill='both')
  • 非常感谢您的回答 :) 但是,这仍然会改变 itemFrame 的大小,因为它会垂直扩展,并且按钮的功能仍然不起作用我也不完全确定我的编辑是否没有通过,但我没有更改 addButtonFrame,而是做了 addButton.pack(expand=1, fill='both'),因为我意识到不需要额外的框架(除非它是?)
  • root.grid_columnconfigure(0, weight=1) root.grid_rowconfigure(1, weight=1)。在AddButton 之外创建和分配item_num = 0 也在函数之外创建item1 = Label(itemFrame) item1.pack()

标签: python tkinter button frame


【解决方案1】:
item_num = 0

def AddButton():
    global item_num #calls global variable
    for x in range(1):                        ###try with for loop
        item_num += 1
        item_text = "Item"+str(item_num) #concatonates text & variable
        item1 = Button(itemFrame, bg="red", text=item_text) #updates label text - doesn't add multiple
        item1.grid()

addButton = Button(itemFrame, text="Add Button", padx=25, pady=25, command=AddButton)
addButton.grid()

f_blank = Frame(root, width=500, height=100)        ####added a blanc frame
f_blank.grid(row=0, column=1)



root.mainloop()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-05-05
    • 1970-01-01
    • 2018-05-11
    • 2021-01-05
    • 2012-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多