Search code examples
pythontkinterpython-3.7tkinter-entry

PYTHON (tkinter) Creating an incremental value in an entry


I am trying to create a program that allows a user to either increment a value or enter it themselves into an entry in a system that looks something like this: The Basic Gui And this is my code

from tkinter import *
ACGui = Tk()

class GUI:
    def __init__(self, start, stop, delay, buttons):
        new_delay = delay
        self.start_button = start
        self.stop_button = stop
        self.delay = StringVar()
        self.new_delay = StringVar()
        self.delay.set(delay)
        self.new_delay.set(new_delay)
        self.buttons = buttons

    def delay_increment(self, op, num):
        print(self.delay.get())
        print(op)
        print(num)

    def create_gui(self):
        Label(ACGui, text="Auto Clicker").grid(row=0, column=0)

        Button(ACGui, text="+1", command=lambda: self.delay_increment("add", 1)).grid(row=1, column=1)

        Label(ACGui, text="Delay: ").grid(row=2, column=0)
        Entry(ACGui, text=self.delay, textvariable=self.new_delay, justify="center").grid(row=2, column=1)
        Button(ACGui, text="Submit", command=self.delay.set(self.new_delay)).grid(row=2, column=5)

        Button(ACGui, text="-1", command=lambda: self.delay_increment("sub", 1)).grid(row=3, column=1)

        ACGui.mainloop()

main = GUI(1, 1, 2, 1)
main.create_gui()

The problem I am having is that the self.delay.get() returns PY_VAR1 OR PY_VAR0

I am truly stuck on what to do here can anyone help me out?


Solution

  • The problem is you dont use lambda on this line

     Button(ACGui, text="Submit", command=self.delay.set(self.new_delay)).grid(row=2, column=5)
    

    It automaticly execute self.delay.set when u run your program. Try

     Button(ACGui, text="Submit", command=lambda:self.delay.set(self.new_delay)).grid(row=2, column=5)