Reputation: 11
Hello guys, please I'm using this code and I want it to increase a variable named
compteur
when I click in the button, I want it to be increased in the__init__
function.but the problem is that this variable stays always to 0
import tkinter as tk
from tkinter import ttk
from PIL import ImageTk, Image
class MainUi(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
self.geometry("300x400")
self.geometry("+500+100")
container = tk.Frame(self)
container.pack(side="top", fill="both", expand=True)
self.frames = {}
frame = StartPage(container, self)
self.frames[StartPage] = frame
frame.place(relx=0, rely=0, relwidth=1, relheight=1)
class StartPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.configure(background='white')
# self.master.configure(background='black')
self.compteur = 0
button1 = ttk.Button(self, text="Next", width=7, command = lambda: self.next_clicked())
button1.place(relx=0.8, rely=0.9)
print(self.compteur)
def next_clicked(self):
self.compteur += 1
app = MainUi()
app.mainloop()
Upvotes: 0
Views: 74
Reputation: 4595
I want it to increase a variable named compteur when I click in the button, I want it to be increased in the init function.
yes it works like this, but i need it to be changed inside the init function because i need to use it in some labels
The problem can be fixed.
__init__
function in StartPage
.configure
for Label widget in next_clicked
function.That all.
Code:
def __init__(self, parent, controller):
:
:
:
self.label_counter = ttk.Label(self, width=5)
self.label_counter.pack()
:
:
:
def next_clicked(self):
self.compteur += 1
self.label_counter.configure(text=self.compteur)
print(self.compteur)
Screenshot:
Upvotes: 0
Reputation: 763
The issue is not that self.compteur
is not raised, but that the print(self.compteur)
statement, which would print the raised value, is placed outside of the next_clicked(self)
method. This should work:
def next_clicked(self):
self.compteur += 1
print(self.compteur)
Upvotes: 1