我正在做班級列表計數器上的程序。我有一個程序,在「人口統計」下面的第5幀中,它表示0,每次點擊「添加到班級列表」按鈕時,它的意思是每增加一次。我嘗試了很多不同的方法,但所有我嘗試過的方法似乎都不起作用。如果有人能提供幫助,這將是值得讚賞的。計算在我的程序中點擊按鈕的次數
這裏是我的代碼,到目前爲止,其餘的我已經爲我的班級名單櫃檯做
import pickle
import os.path
from tkinter import *
import tkinter.messagebox
import tkinter as tk
class Class:
def __init__(self, firstname, lastname):
self.firstname = firstname
self.lastname = lastname
class ClassPopulation():
def __init__(self):
window = Tk()
window.title("Class population")
self.firstnameVar = StringVar()
self.lastnameVar = StringVar()
frame1 = Frame(window)
frame1.pack()
Label(frame1, text = "First name").grid(row = 1,
column = 1, sticky = W)
Entry(frame1, textvariable = self.firstnameVar,
width = 40).grid(row = 1, column = 2)
frame2 = Frame(window)
frame2.pack()
Label(frame2, text = "Last name").grid(row = 1, column = 1, sticky = W)
Entry(frame2, textvariable = self.lastnameVar,
width = 40).grid(row = 1, column = 2)
frame3 = Frame(window)
frame3.pack()
Button(frame3, text = "Add to classlist",
command = self.processAdd).grid(row = 1, column = 1)
frame4 = Frame(window)
frame4.pack()
Label(frame4, text = "Population Count").grid(row = 1, column = 1, sticky = W)
frame5 = Frame(window)
frame5.pack()
Label(frame5, text = "0").grid(row = 1, column = 1, sticky = W)
population = 0
def population(label):
population = 0
def mbutton():
global population
population +=1
label.config(text=str(population))
self.classList = self.loadClass()
self.current = 0
if len(self.classList) > 0:
self.setClass()
def saveClass(self):
outfile = open("Population.dat", "wb")
pickle.dump(self.classList, outfile)
tkinter.messagebox.showinfo("Class Population","New name registered")
outfile.close()
def loadClass(self):
if not os.path.isfile("Population.dat"):
return [] # Return an empty list
try:
infile = open("Population.dat", "rb")
classList = pickle.load(infile)
except EOFError:
classList = []
infile.close()
return classList
def processAdd(self):
classList = Class(self.firstnameVar.get(), self.lastnameVar.get())
self.classList.append(classList)
self.saveClass()
def setClass(self):
self.firstnameVar.set(self.classList[self.current].firstname)
self.lastnameVar.set(self.classList[self.current].lastname)
ClassPopulation()
你能解釋,在編碼格式?謝謝 –
如果您的意思是添加一個像「def get_population(self):return self.population」這樣的無用方法,那麼核心python開發人員通常認爲這是不好的做法,從Guido van Rossum開始。 –