2016-08-18 103 views
1

我正在做班級列表計數器上的程序。我有一個程序,在「人口統計」下面的第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() 

回答

3

而不是使用全球​​你需要在你__init__方法聲明self.population = 0和遞增self.population在你的按鈕點擊處理器。使用類方法來獲取價值通常也被認爲是很好的做法。

+0

你能解釋,在編碼格式?謝謝 –

+1

如果您的意思是添加一個像「def get_population(self):return self.population」這樣的無用方法,那麼核心python開發人員通常認爲這是不好的做法,從Guido van Rossum開始。 –

1

可能的解決方案:如果你想要做的是,有人口數量,並增加它在每個按鈕上點擊:

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 
     self.population = 0 # made it a class attr 

    # def population(label): 
    #  population = 0 

    def add_population(self): 
     # having it separate can help in modification or enhancement 
     self.population +=1 

    def mbutton(): 
     # global population 
     # population +=1 
     self.add_population() # increment 
     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() 

編輯1:我已經添加了完整代碼,以及我的建議更改 - 以前的語句會被評論,所以您知道這些更改。

+0

這樣做我認爲需要2個解決方案在同一時間。你如何做到這一點,或許在編碼格式將有所幫助。 –

+0

使羣體成爲一個類屬性,使其更加優雅,並將增量方法分開設置。在mbutton()而不是直接增量中,調用了add_population() - 因爲如果需要,可以在其中添加其他語句。總體來說,它與上面的代碼相同,只是添加/編輯了2 3行。 P.S.我沒有完全評論你的評論 - 你想要完整的代碼嗎? –

+0

是的,我會愛完整的代碼 –