2016-08-12 5 views
0

私はクラスの人口カウンターを開発しようとしています。問題は、私がそれを実行すると2つのプログラムが出てくることです。私は自分のプログラムをすべて一つにしたいと思っていました。カウンターは別のプログラムに入ってきて、実際のプログラムには転送できません。これはどうすればいいですか?ここでクラス母集団カウンタ。プログラムをすべて1つにまとめる方法

は、私は2つのウィンドウが予定されていると思う私は、Python

import pickle 
import os.path 
from tkinter import * 
import tkinter.messagebox 
import tkinter as tk 

population = 0 
def counter_label(label): 
    population = 0 
def count(): 
    global population 
    population +=1 
    label.config(text=str(population)) 
root = tk.Tk() 
label = tk.Label(root) 
label.pack() 
counter_label(label) 
button = tk.Button(root, text='Population Count', command=count).pack() 
root.mainloop() 

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) 


     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

を使用しています、私の添付ファイルであるプログラムは二回Tk()を実行するためである - 1 root = tk.Tk()window = Tk()で別のものを。 root TkinterインスタンスをクラスClassPopulationに渡すと、1つのウィンドウが表示されます。

[EDIT]

class Class: 
    def __init__(self, firstname, lastname): 
     self.firstname = firstname 
     self.lastname = lastname 

class ClassPopulation: 
    def __init__(self, root_window): 
     window = self.root_window 
     window.title("Class population") 

population = 0 

def counter_label(label): 
    population = 0 

def count(): 
    global population 
    population +=1 

label.config(text=str(population)) 
root = Tk() 
label = tk.Label(root) 
label.pack() 
ClassPopulation(root) 
counter_label(label) 

root.mainloop() 
+0

あなたはデフ 'に'、私はこれを行うのですかでより詳細に 'ClassPopulation'、編集' __init __(自己)でコーディング –

+0

のおそらく構造を説明することができています__init __(self、root_window) 'を実行し、' window = self.root_window'を設定します。 'ClassPopulation'を呼び出すとき、' ClassPopulation(root) 'として' root'を渡します - ここで 'root'は' root = Tk() 'です。また、スクリプト全体を一緒に保つようにしてください。それは良いプログラミング実践です。 – stuartnox

+0

だから私のコーディングが、この今 –

関連する問題