2017-09-18 19 views
-1

True機能で追加した後、tkinterウィンドウが開かない。これをどのように機能させることができますか?真実がなくても動作しますが、私の機能にはそれが必要です。あなたが持っていた2つのエラーTkinterウィンドウが開かれていない

from tkinter import * 
from random import random 
import sys 
import random 



maxcount = int (input("How many times ")) 
i = 1 
cats = Tk() 
cats.wm_title("maxcount test") 
cats.geometry("500x500") 

def black(): 
    while True: 
     i+1 
     if i == 5: 
      break 

Button(cats, text="Start", command=black()).grid(row=1, column=0) 


Label(cats, text="How many times:").grid(row=0, column=0) 

cats.mainloop() 
+1

あなたは 'while'ループを持っている必要があります理由はありますか?あなたがやっていることは、変数を5に設定しているだけです。これには、whileループを行う必要はありません。 'while'ループを必要としているものがありますか? –

答えて

1


- i + 1はおそらくi += 1を意味し、それは関数のスコープでmodiciedすることができますので、その後、iカビがglobal宣言します。
- Buttonコマンドはblack()でした。これは、関数blackの呼び出しです。必要とされているもの(()なし)機能blackへの参照です

もう一つ注意すべき:@Sierra_Mountain_Techで述べて、それがあるとして、ユーザーが開始する Tkinterのアプリのための整数最初に入力しなければなりません。

from tkinter import * 
from random import random 
import sys 
import random 

maxcount = int (input("How many times ")) 
i = 1 

cats = Tk() 
cats.wm_title("maxcount test") 
cats.geometry("500x500") 

def black(): 
    global i 
    while True: 
     i += 1 
     if i >= 5: # <-- changed from i == 5 at @Sierra_Mountain_Tech suggestion 
      break 

Button(cats, text="Start", command=black).grid(row=1, column=0) 
Label(cats, text="How many times:").grid(row=0, column=0)  

cats.mainloop() 
+0

また、「開始」を2回以上押すと、プログラムがフリーズします。 'i == 5'を' if i> = 5'に変更する –

+1

良いキャッチ、ありがとう。 –

+1

最後にあなたの答えに追加したいことがあります。ユーザは、tkinterアプリケーションの起動前に 'input'を使用しているので、プログラムはコンソールに整数を入力するまで開かれません。彼らは少なくともtkinterには新しく見えるので、これは気づいていないかもしれません。 –

関連する問題