Reputation: 25
I am making a test program using Tkinter, but I'm running into some trouble.
I'm trying to make a Rock, Paper, Scissors game on which the users always loses, but I get "Label has no call instance", here's the code:
#Rock papers & scissor
import Tkinter as tk
class App(tk.Tk):
def setLabel(self):
entry2 = self.entry.get()
if entry2 == "paper":
self.l1(App, text="Scissors, you loose!")
elif entry2 == "Paper":
self.l1(text="Scissors, you loose!")
elif entry2 == "PAPER":
self.l1(text="Scissors, you loose!")
elif entry2 == "rock":
self.l1(text="Paper, you loose!")
elif entry2 == "Rock":
self.l1(text="Paper, you loose!")
elif entry2 == "ROCK":
self.l1(text="Paper, you loose!")
elif entry2 == "Scissors":
self.l1(text="Rock, you loose!")
elif entry2 == "scissors":
self.l1(text="Rock, you loose!")
elif entry2 == "SCISSORS":
self.l1(text="Rock, you loose!")
elif entry2 == "scissor":
self.l1(text="Rock, you loose!")
elif entry2 == "Scissor":
self.l1(text="Rock, you loose!")
elif entry2 == "SCISSOR":
self.l1(text="Rock, you loose!")
else:
self.l1(text="wat")
def __init__(self):
tk.Tk.__init__(self)
self.entry = tk.Entry(self)
self.btn = tk.Button(self, text="Go!", command=self.setLabel)
self.l1 = tk.Label(self, text="Waiting...")
self.entry.pack()
self.btn.pack()
self.l1.pack()
App().mainloop()
App().minsize(300, 100)
App().title("Test")
Upvotes: 0
Views: 1200
Reputation: 49330
A few problems:
self.l1(text='mytext')
is incorrect. When you put the parentheses right after self.l1
, you're trying to call that label like it's a function, but it's not a function. You have to call that label's config()
method.App
object and loop it... then you create another, and another. Create one, save a reference, and then work with it.if
branch was larger than it could be.mainloop()
call should be at the end.The following fixes these problems:
import Tkinter as tk
class App(tk.Tk):
def setLabel(self):
entry2 = self.entry.get().lower()
if entry2.startswith('p'):
self.l1.config(text="Scissors, you lose!")
elif entry2.startswith('r'):
self.l1.config(text="Paper, you lose!")
elif entry2.startswith('s'):
self.l1.config(text="Rock, you lose!")
else:
self.l1.config(text="wat")
def __init__(self):
tk.Tk.__init__(self)
self.entry = tk.Entry(self)
self.btn = tk.Button(self, text="Go!", command=self.setLabel)
self.l1 = tk.Label(self, text="Waiting...")
self.entry.pack()
self.btn.pack()
self.l1.pack()
app = App()
app.minsize(300, 100)
app.title("Test")
app.mainloop()
Upvotes: 1