私はtkinterを学ぼうとしていて、3つのボタンと1つのテキストウィジェットを備えたウィンドウがある簡単なじゃんけんゲームを書きたかったのです。
いずれかのボタンを押してメッセージをテキストフィールドに表示し、別のボタンをクリックしたり、テキストフィールドをクリックして、2番目のボタンに関連付けられた新しいメッセージをクリアして表示したりできるようにしたいと思います。
私が見たチュートリアルから、ボタンコマンドパラメータの引数としてテキストウィジェットを格納する関数を渡すことができることを知っています。テキストフィールドを使用して、ボタンごとに1つずつ(一度に1つずつ)3つの関数を作成できることを知っています。しかし、それはおそらく正しい方法ではありません。これが私がこれまでに持っているものです:
import tkinter as tk
root = tk.Tk()
root.title("Rock Paper Scissors")
root.geometry("420x200")
def Rock():
rockText = "Paper!"
return rockText
def Paper():
paperText = "Scissors!"
return paperText
def Scissors():
scissorsText = "Rock!"
return scissorsText
def display():
textDisplay = tk.Text(master = root, height = 10, width = 50)
textDisplay.grid(row = 1, columnspan = 5)
textDisplay.insert(tk.END, Rock())
buttonRock = tk.Button(text = "Rock", command = display).grid(row = 0, column = 1, padx = 10)
buttonPaper = tk.Button(text = "Paper").grid(row = 0, column = 2, padx = 10)
buttonScissors = tk.Button(text = "Scissors").grid(row = 0, column = 3, padx = 10)
root.mainloop()
どんな助けでもありがたいです。
編集:2番目の考え-ゲームをこのように動作させることを試みることによって、私はこれを自分自身のために複雑にしていると想像できます。ランダムモジュールを使用すると、リストを使用してコンピューターを選択するための1つの関数を使用せずに、ランダムピックをパラメーターに保存してから、値を表示関数に返すことができます。
したがって、これが正しければ、ボタンをクリックしてテキストウィジェットのテキストを変更するだけです。そのためには、2つの簡単で非常によく似たオプションがあります。まず、あなたが行ったように3つの関数を定義し、それらにテキストを直接変更させることです。2番目のオプションは、与えられた内容に従ってテキストを変更する1つの関数を作成することです。2番目のケースでは、これを使用する必要があることに注意してください。これは、lambda
小規模なプロジェクトでは非常にうまく機能しますが、プログラムが大きくなるとプログラムの効率が低下します。
最初のオプション:
import tkinter as tk
class App:
def __init__(self):
root=tk.Tk()
root.title("Rock Paper Scissors")
root.geometry("420x200")
self.text=Text(root)
self.text.grid(row=1,columnspan=5)
tk.Button(root,text="Rock",command=self.Rock).grid(row=0,column=1,padx=10)
tk.Button(root,text="Paper",command=self.Paper).grid(row=0,column=2)
tk.Button(root,text="Scissors",command=self.Scissors).grid(row=0,column=3,padx=10)
root.mainloop()
def Rock(self):
text="Paper!"
self.text.delete(0,END) #delete everything from the Text
self.text.insert(0,text) #put the text in
def Paper(self):
text="Scissors!"
self.text.delete(0,END) #delete everything from the Text
self.text.insert(0,text) #put the text in
def Scissors(self):
text="Rock!"
self.text.delete(0,END) #delete everything from the Text
self.text.insert(0,text) #put the text in
if __name__=='__main__':
App()
2番目のオプション:
import tkinter as tk
class App:
def __init__(self):
root=tk.Tk()
root.title("Rock Paper Scissors")
root.geometry("420x200")
self.text=Text(root)
self.text.grid(row=1,columnspan=5)
tk.Button(root,text="Rock",command=lambda: self.updateText('Paper!')).grid(row=0,column=1,padx=10)
tk.Button(root,text="Paper",command=lambda: self.updateText('Scissors!')).grid(row=0,column=2)
tk.Button(root,text="Scissors",command=lambda: self.updateText('Rock!')).grid(row=0,column=3,padx=10)
root.mainloop()
def updateText(self,text):
self.text.delete(0,END) #delete everything from the Text
self.text.insert(0,text) #put the text in
if __name__=='__main__':
App()
ここで私からのいくつかの小さなサイドノート:
grid
、pack
またはplace
右ウィジェット自体には、変数へのウィジェットが、リターンの割り当て文句を言わないgrid
、pack
またはplace
ある機能をNone
。したがって、最初にウィジェットを変数に割り当ててから、Text
-widgetの場合と同じようにジオメトリマネージャーを使用します。className
-argumentで設定できTk
ます。Tk
ウィンドウを開いているならそれはあなたがそれを取りたいものではないかもしれないものを自動的に選択します。お役に立てば幸いです。プログラミングを楽しんでください!
編集:ランダムモジュールであなたの編集を見たところです。この場合、2番目のオプションをお勧めします。ただ、削除text
から-argumentをupdateText
して置き換えるlambda: self.updateText(...)
とself.updateText()
。それupdateText
自体で、あなたが言及したリストのランダムなものを追加します。:D
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加