0
이것은 내 첫 번째 질문은 Stackoverflow입니다. 저는 파이썬을 당분간 배우고 있습니다 만, 실제로 코드에 어떤 문제가 있는지 알지 못합니다.파이썬 3.3 기본 10 int()에 대한 잘못된 리터럴
from tkinter import *
import random
class Application(Frame):
""" GUI application for the "Guess my number" game"""
def __init__(self, master):
"""Initialize Frame. """
super(Application, self).__init__(master)
self.the_number = random.randint(1,100)
self.grid()
self.create_widgets()
def create_widgets(self):
"""Create widgets to get the number and show if it's correct"""
# create instruction label
Label(self,
text = "I'm thinking of a number between 1-100. Can you guess it?"
).grid(row = 0, column = 0, columnspan = 2, sticky = W)
# create label and text entry for the guess
Label(self,
text = "Guess: "
).grid(row = 1, column = 0, sticky = W)
self.guess = Entry(self)
self.guess.grid(row = 1, column = 1, sticky = W)
# create a text field
self.correct_txt = Text(self, width = 40, height = 5, wrap = WORD)
self.correct_txt.grid(row = 3, column = 0, columnspan = 2)
# create a submit button
Button(self,
text = "Click to submit",
command = self.check_number()
).grid(row = 2, column = 0, sticky = W)
def check_number(self):
# get the guess of the user
guess = int(self.guess.get())
# see if the guess is correct or wrong
if guess == self.the_number:
text = ("Congratulations, you guessed it! And it only took you", tries, "tries!")
self.correct_txt.delete(0.0, END)
self.correct_txt.insert(0.0, text)
elif guess < self.the_number:
text = "Higher..."
self.correct_txt.delete(0.0, END)
self.correct_txt.insert(0.0, text)
elif guess > self.the_number:
text = "Lower..."
self.correct_txt.delete(0.0, END)
self.correct_txt.insert(0.0, text)
# main
root = Tk()
root.title("Guess the number")
app = Application(root)
root.mainloop()
누군가가 뭐가 잘못 됐는지 말해 줄 수 :
Traceback (most recent call last):
File "D:\Python\guess the number gui.py", line 63, in <module>
app = Application(root)
File "D:\Python\guess the number gui.py", line 14, in __init__
self.create_widgets()
File "D:\Python\guess the number gui.py", line 37, in create_widgets
command = self.check_number()
File "D:\Python\guess the number gui.py", line 44, in check_number
guess = int(self.guess.get())
ValueError: invalid literal for int() with base 10: ''
이 코드는 : 내 프로그램을 실행할 때마다, 나는이 오류가? 미리 감사드립니다!
그러면 'guess' 텍스트 상자에 무엇을 입력 했습니까? 올바른 번호가 아닙니다. –
분명히 텍스트 상자에 빈 줄을 입력했는데, 본질적으로 정수로 변환 할 수는 없습니다. – favoretti
입력 확인 중 : http://stackoverflow.com/questions/8959815/restricting-the-value-in-tkinter-entry-widget – kalgasnik