2016-09-22 16 views
1

背景情報以下PythonのTkinterの - ループ

前に実行ウィンドウは、私は現在、基本的に単語を出力するプログラムを作成するために働いているコードであり、かつ正確に単語のスペルをお願いします。一度使用されるとusedWords辞書に入れられて再利用されないので、単語は一度だけ印刷できます。すべての単語が使い尽くされると、whileループが終了します。これは完全に機能しますが、今はGUIと統合しようとしています。

特に何私の問題は、問題

であることは、私はTkinterのウィンドウがラベルを持っている、とラベルの更新プログラムを新しい単語がプリントアウトされるたびに持っていたいということです。しかし、現在エラーが発生しています AttributeError: 'NoneType'オブジェクトに '_root'属性がありません メインウィンドウを定義する前にラベルを追加しようとしたため、これが起こっていると思います。ループが終了する前にウィンドウが開いていないループの前にウィンドウが定義されている場合は、これは他の方法で行います。これは明らかに私が実際にやりたいことに反するものです。私はこの作品を作るのですかどのように私の質問

それはthis-と同じくらい簡単ですがsaid-そのすべてで

?私は、ループの前にウィンドウを作成したい、とウィンドウ更新内のラベルを持つ新しい単語がリストから

import random 
import time 
from tkinter import * 
from tkinter.messagebox import * 

words = ['reflection', 'attitude', 'replicate', 'accomodate', 'terrain', 
     'arguemental', 'stipulate', 'stimulation', 'latitude', 'marginal', 
     'thedubester', 'security', 'documentation', 'ukulele', 
     'terminal', 'exaggeration', 'declaration', 'apptitude', 'absence', 
     'aggressive', 'acceptable', ' allegiance', 'embarass', 'hierarchy', 
     'humorous', 'existence'] 

usedWords = [] 

while len(usedWords) < len(words): 
    chooseWord = words[random.randrange(len(words))] 

    var = StringVar() 
    display = Label(textvariable = var) 
    display.place(x=1,y=1) 

    if chooseWord not in usedWords: 


     var.set(chooseWord) 
     userSpelt = input("Spelling: ") 
     usedWords.append(chooseWord) 

     if userSpelt in words: 
      print("Correctly spelt!") 
     else: 
      print("Incorrectly spelt!") 



master = Tk() 
master.geometry("500x600") 
master.title("Minesweeper V 1.0") 
master.configure(background="#2c3e50") 

答えて

2

を選択するたびに、私はあなたに実行している問題の1つだと思いますコンソール入力からダイアログボックスを作成しようとしています。私はそれを行う方法があると確信していますが、現時点で私は良い方法を考えることができません。

別の問題は、あなたの単語リストにある単語の数を超えた後まで、実際にウィンドウを作成していないことです(スクリプト内のマスター)。

Tkinterはコンソールスクリプトとは少し異なります。イベントベースなので、アプリケーション内でwhileループを書くと、tkinterはループが完了するまで「一時停止」します。

代わりに、ループをtkinterで行い、その周りをコードすることができます。ここで私が思いついた簡単な実装があります。それはまだ作業が必要が、それは(あなたがすべて小文字で「Tkinterのを」使用しているので、私はあなたのpython 3を使用していると仮定しています)仕事を取得します。

import random 
from tkinter import * 
from tkinter.messagebox import * 

words = ['reflection', 'attitude', 'replicate', 'accomodate', 'terrain', 
     'arguemental', 'stipulate', 'stimulation', 'latitude', 'marginal', 
     'thedubester', 'security', 'documentation', 'ukulele', 
     'terminal', 'exaggeration', 'declaration', 'apptitude', 'absence', 
     'aggressive', 'acceptable', ' allegiance', 'embarass', 'hierarchy', 
     'humorous', 'existence'] 

usedWords = [] 

class App(Tk): 
    def __init__(self): 
     super().__init__() 

     self._frame = Frame(self) 
     self._frame.pack() 

     self._label = Label(self, text="") 
     self._label.pack() 

     self._entry_var = StringVar() 
     self._entry = Entry(self, textvariable=self._entry_var) 
     self._entry.pack() 

     self._button = Button(
      self, 
      text="Check", 
      command=self.check_callback 
     ) 
     self._button.pack() 

     self.get_new_word() 

    def check_callback(self): 
     if self._current_word == self._entry.get(): 
      showinfo("Correct", "Correctly spelled.") 
      usedWords.append(self._current_word) 
     else: 
      showwarning("Incorrect", "Incorrectly spelled.") 
      words.append(self._current_word) 
     self.get_new_word() 

    def get_new_word(self): 
     self._current_word = words.pop(random.randrange(len(words))) 
     self._label.configure(text=self._current_word) 
     self._entry_var.set("") 
     self.title(self._current_word) 

app = App() 
app.mainloop() 

は、これはクラスにTkinterの部分を移動させTkクラスから継承します。私の意見では、プログラムが複雑になると、作業が楽になります。

+0

ありがとう、これは完璧です。私がしたいことのための良い出発点。私のエッセイを読む時間をとってくれてありがとう。 1つの質問、私はクラスに新しくて素朴です - これで使ったクラス関連の関数をすべて説明するクラスを知っていますか? super().__ init __()など – Csarg

+0

また、あなたが設定したself._buttonの代わりにRETURNボタンを持つためにbind関数を使う方法はありますか? – Csarg

+0

[クラスのためのPythonドキュメント]を見てくださいhttps://docs.python.org/3.6/tutorial/classes.html)を参照してください。"スーパー"は親クラスのメソッドを呼び出す方法であり、もう少し説明されています(https://docs.python.org/3.6/library/functions.html#super)。 補足として、無料の[電子ブックfrom Python](https://www.packtpub.com/tech/python) –

関連する問題