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()
问题不是没有提出
self.compteur
,而是将打印提出的值的print(self.compteur)
语句放在next_clicked(self)
方法之外。这应该起作用:相关问题 更多 >
编程相关推荐