【问题标题】:Adding button (with variables) by pressing button - tkinter通过按下按钮添加按钮(带变量) - tkinter
【发布时间】:2021-07-02 13:21:03
【问题描述】:

我正在制作一个销售点系统并尝试实现一个按钮,当按下一个新按钮时,还会出现一个要求用户输入项目的窗口

def newButton ():
    w = Toplevel()
    w.title("New Item") #creates new window when button is pressed
    w.geometry("200x200")
    
    itemNameLabel = Label(w, font=("arial", 15), text="What is the item called?")
    itemNameLabel.grid(row=0, padx=5)
    itemName = Entry(w, width=18,  borderwidth=5)
    itemName.grid(row=1, padx=5)
    newItemName = itemName.get

    itemPriceLabel = Label(w, font=("arial", 15), text="What is the item's price?")
    itemPriceLabel.grid(row=4, padx=5)
    itemPrice = Entry(w, width=18,  borderwidth=5)
    itemPrice.grid(row=5, padx=5)

    def item6_Button():
        global item6_qty
        item6_price = itemPrice.get
        item6_text = newItemName
        item6_qty += 1
        item6_text = (item6_text + "    "+str(item6_price) +"    "+ str(item6_qty)) #concatonates text & variable
        item6.config(text=item6_text) #updates label text - doesn't add multiple 
        item6.pack()

        item6_Button = Button(itemFrame, text=newItemName, width=10, height=5, command=item6_Button)
        item6_Button.grid(row=7, column=1, padx=5)
        item6 = Label(receiptFrame)
    w.mainloop()


newButton= Button(itemFrame, text="Add New Button", width=20, height=5, command=newButton) #creates button for new window
newButton.place(x=480, y=600)
newButton = Label(itemFrame)

*item6_qty 和 item6_price 在程序开头附近声明

这是我目前所拥有的,尽管出现了窗口,但我认为变量实际上并未设置在项目框架中出现的新按钮之上。我不完全确定该怎么做 - 我需要使用 .insert 作为变量吗?

这是创建普通按钮的标准代码

#Item1 Button + Function
def item1_Button():
    global item1_qty #making qty variable global so it can used
    item1_text = ("Chips")
    item1_qty += 1 #increments qty variable by one everytime button is clicked
    item1_text = (item1_text + "    "+str(item1_price) +"    "+ str(item1_qty)) #concatonates text & variable
    item1.config(text=item1_text) #updates label text - doesn't add multiple 
    item1.pack() #places label within the frame
    
    
item1_Button = Button(itemFrame, text="Chips", width=10, height=5, command=item1_Button)
#creates button + links to function
item1_Button.grid(row=4, column=1, padx=5) #positions button
item1 = Label(receiptFrame)#creates label for button

我不确定我是否提供了足够的代码来说明我所做的工作,以便更好地了解我正在努力实现的目标,但我知道大块代码并不是很受欢迎

【问题讨论】:

    标签: python variables tkinter button window


    【解决方案1】:

    这是一个你可以做什么的例子(这有帮助吗?):

    from tkinter import Tk, Button, Entry, Toplevel
    
    
    class MainWindow(Tk):
        def __init__(self):
            Tk.__init__(self)
    
            self.geometry('100x150')
    
            self.btn = Button(self, text='Create New!', command=self.ask)
            self.btn.pack()
    
        def ask(self):
            ask_window = InputWindow(self)
            ask_window.focus_force()
        
        def create(self, text):
            button = Button(self, text=text)
            button.pack()
        
    
    class InputWindow(Toplevel):
        def __init__(self, parent):
            Toplevel.__init__(self, parent)
            self.parent = parent
            self.bind('<FocusOut>', self.destroy_)
    
            self.user_input = Entry(self)
            self.user_input.pack()
            
            self.submit_btn = Button(self, text='Submit!', command=self.retrieve)
            self.submit_btn.pack()
            
        def retrieve(self):
            text = self.user_input.get()
            self.parent.create(text)
            self.destroy()
    
        def destroy_(self, event):
            if isinstance(event.widget, Toplevel):
                self.destroy()
            
    
    root = MainWindow()
    root.mainloop()
    

    【讨论】:

    • 你好!非常感谢你做的这些!!它完美地工作!你能解释一下InputWindow、retrieve和destroy函数的作用吗?我对课程不是很熟悉,所以有些事情并不完全有意义啊哈^^
    • @STRhythm InputWindow 是一个类,但为了便于理解,您可以假设它是一个包含相关函数和变量的篮子。它有一个方法retrieve,它允许从“篮子”变量中获取信息,特别是一个条目,所以基本上它只是从条目框中获取信息。如图所示的破坏部分不是必需的,我只是希望它能够在窗口的焦点转移到其他地方时关闭,所以基本上你不会得到大量无用的窗口。您还不如在按下 X 时破坏窗口
    • 我希望我说的很清楚,但是如果您仍然不明白,请随时问,here 是一个关于课程的教程,所以我建议您观看它。
    • @STRhythm if You are ask about this part: ask_window = InputWindow(self) 它只是启动类 InputWindow,它不是一个函数,它是一个类,类似于说 root = Tk() 或 @987654326 @
    • 这里还有一些我通常用来获取小部件信息的 tkinter 资源:this
    猜你喜欢
    • 2017-12-02
    • 1970-01-01
    • 2021-11-11
    • 1970-01-01
    • 2015-12-31
    • 2023-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多