Witam,
Mam taki kod, w którym mam problem z przejściem z terminalu na OOP, chodzi o random.
Program ma za zadanie losować liczby w GUI, poprzez kliknięcie przycisku "Losuj".
Wywala problem:
File "/home/elvis/PycharmProjects/untitled12/12.py", line 55, in reveal
if odgadnieta > wynik:
TypeError: unorderable types: str() > int()
# Program jaka to liczba
# zrobione na okienkach GUI
from tkinter import *
import random
class Application(Frame):
def __init__(self, master):
super(Application, self).__init__(master)
self.grid()
# self.bttn_clicks = 0 # liczba klikniec buttona
self.create_widgets()
# def losuj(self):
# the_number = random.randint(1, 100)
# guess = int(input("Ta liczba, to: "))
# tries = 1
# while guess != the_number:
# if guess > the_number:
# print("Za duza...")
# else:
# print("Za mala...")
# guess = int(input("Ta liczba to: "))
# tries += 1
#
def create_widgets(self):
# utworz etykiete do liczby wynikowej
self.pw_lbl = Label(self, text = "Liczba: ")
self.pw_lbl.grid(row = 1, column = 0, sticky = W)
# utworz Entry do przejecia liczby
self.pw_ent = Entry(self)
self.pw_ent.grid(row=1, column=0, sticky=W)
# utworz przycisk losuj
self.subbmit_bttn = Button(self, text="Losuj", command = self.reveal)
self.subbmit_bttn.grid(row = 2, column = 0, sticky = W)
# utwórz widżet Text do wyświetlenia komunikatu
self.secret_txt = Text(self, width = 35, height = 5, wrap = WORD)
self.secret_txt.grid(row = 3, column = 0, columnspan = 2, sticky = W)
def update_count(self):
self.bttn_clicks += 0
self.bttn1["text"] = "Losuj dalej: " + str(self.bttn_clicks)
# self.bttn1["command"] = self.losuj()
def reveal(self):
# pw_ent.get -> przejecie liczby
wynik = random.randint(1, 100)
odgadnieta = self.pw_ent.get()
proba = 1
while odgadnieta != wynik:
if odgadnieta > wynik:
self.secret_txt.delete(0.0, END)
self.secret_txt.insert(0.0, "Za duza...")
else:
self.secret_txt.delete(0.0, END)
self.secret_txt.insert(0.0, "Za mala...")
odgadnieta = self.pw_ent.get()
proba += 1
# self.secret_txt.delete(0.0, END)
# self.secret_txt.insert(0.0, message)
# return wynik
# część główna
root = Tk()
root.title("Jaka to liczba?")
root.geometry("300x150")
app = Application(root)
root.mainloop()