Python - Ejercicio con Tkinter, Python

 
Vista:
sin imagen de perfil

Ejercicio con Tkinter, Python

Publicado por Nicholas (3 intervenciones) el 24/11/2021 03:31:22
Hola Buenas, tengo una duda con Tkinter, resulta que debo hacer un juego, estuve programando bastante tiempo pues soy apenas principiante en la programación, pero por alguna razón, no logro hacer que los botones cumplan los comandos, y ya llevo 2 horas viendo videos y leyendo documentos de Python y no sé como seguir avanzando jaja, espero si alguien pudiese darme aunque sea un empujoncito para seguir desarrollando. Un saludo, y gracias.

Les dejo el codigo:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
from tkinter import *
from tkinter import ttk
import tkinter as tk
import random
 
# FUNCIONES
 
def numerorandom():
  x = random.randint(1,100)
 
def ingresar():
  ingresar = int(ingresarNum.get())
  return dialogo()
  borrar()
 
def borrar():
  ingresarNum.set('')
 
def dialogo():
  if ingresar > x:
    r.set(str("El numero ingresado es mayor que el digito secreto"))
  if ingresar < x:
    r.set(str("El numero ingresado es menor que el digito secreto"))
  if ingresar == x:
    r.set(str("Has encontrado el numero secreto."))
 
 
# INTERFAZ
 
ventana = Tk()
ventana.geometry('400x300')
ventana.title ('Adivina mi numero')
ventana.configure(bg = 'azure4')
 
# VARIABLES
 
r = tk.StringVar()
ingresar = tk.StringVar()
 
# LABELS
 
label = tk.Label(ventana, text = 'Ingrese su numero')
label.pack(anchor = NW)
label.place(x = 10, y = 10)
label.config(fg = 'snow', bg = 'azure4', font = ('Bodoni, 15'))
 
cuadro = tk.Label(ventana, text = 'Cuadro de dialogo')
cuadro.pack()
cuadro.place(x = 10, y = 130)
cuadro.config(fg = 'red4', bg = 'azure4')
 
dialogo = tk.Label(ventana, textvariable = r)
dialogo.pack()
dialogo.place(x = 5, y = 160)
dialogo.config(fg = 'red4', bg = 'azure4')
 
 
# ENTRY
 
entry = Entry(ventana)
entry.pack(anchor = NW)
entry.place(x = 10, y = 40)
 
# BOTONES
 
comenzar = tk.Button(ventana, text = 'Iniciar Juego', command = numerorandom)
comenzar.pack()
comenzar.place(x = 20, y = 80)
 
ingresarNum = tk.Button(ventana, text = 'Ingresar', command = ingresar)
ingresarNum.pack()
ingresarNum.place(x = 140, y = 80)
 
# BUCLE
 
ventana.mainloop()
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder

Ejercicio con Tkinter, Python

Publicado por akn (1 intervención) el 29/06/2022 21:51:00
Hola resolvi el código de su juego por si le sirva:

from tkinter import *
import tkinter as tk
import random

# FUNCIONES

def numeroaleatorio():
global numal
numal=random.randint(1,100)
return numal

def ingresar(a,b):
if a > b:
return r.set("El numero ingresado es mayor que el digito secreto")
if a < b:
return r.set("El numero ingresado es menor que el digito secreto")
if a == b:
return r.set("Has encontrado el numero secreto.")

def juego():
x=int(entry.get())
rand=numal
ingresar(x,rand)

# INTERFAZ

ventana = Tk()
ventana.geometry('300x160')
ventana.title ('Adivina mi numero')
ventana.configure(background= 'azure4')

# VARIABLES

r = StringVar()
numal=IntVar()

# LABELS

label = Label(ventana, text = 'Ingrese su numero entre 1 y 100')
label.pack(anchor = NW)
label.place(x = 10, y = 10)
label.config(fg = 'snow', bg = 'azure4', font = ('Bodoni, 15'))

dialogo = Label(ventana, textvariable = r)
dialogo.place(x = 10, y = 130)
dialogo.config(fg = 'red4', bg = 'azure4')

# ENTRY

entry = Entry(ventana)
entry.pack(anchor = NW)
entry.place(x = 10, y = 40)

# BOTONES

comenzar = tk.Button(ventana, text = 'Iniciar Juego', command = numeroaleatorio)
comenzar.place(x = 20, y = 80)

ingresarNum = tk.Button(ventana, text = 'Ingresar', command = juego)
ingresarNum.place(x = 140, y = 80)

# BUCLE

ventana.mainloop()
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar

Solucion del ejercicio usando POO

Publicado por Gustavo (1 intervención) el 25/07/2023 05:30:20
import tkinter as tk
from tkinter import ttk
import random

class Ventana:
def __init__(self, ventana):
self.ven = ventana
self.ven.title('Adivina mi número')
self.ven.geometry('400x300')
self.ven.configure(bg='azure4')
self.r = tk.StringVar()

style = ttk.Style()
style.configure("My.TFrame", background="lightblue")

l1 = ttk.Label(self.ven, text='Ingrese su número: ', background="lightblue",
foreground='snow' ,font=('bodoni', 15))
l1.place(x=10, y=10)

self.e1 = ttk.Entry(self.ven)
self.e1.place(x=10, y=40)

l2 = ttk.Label(self.ven, text='Cuadro de diálogo', background="lightblue",
foreground='red4', font=('bodoni', 15))
l2.place(x=10, y=130)

l3 = ttk.Label(self.ven, textvariable=self.r, background="lightblue",
foreground='red4', font=('bodoni', 15))
l3.place(x=5, y=160)
comenzar = tk.Button(ventana, text='Iniciar Juego', command=self.numeroRandom)
comenzar.place(x=20, y=80)

ingresarNum = tk.Button(ventana, text='Ingresar', command=self.juego)
ingresarNum.place(x=140, y=80)

def numeroRandom(self):
global x
x = random.randint(1,100)
return x

def ingresar(self,a,x):
if a > x:
self.r.set(str("El numero ingresado es mayor que el digito secreto"))
if a < x:
self.r.set(str("El numero ingresado es menor que el digito secreto"))
if a == x:
self.r.set(str("Has encontrado el numero secreto."))
def juego(self):
a=int(self.e1.get())
b= x
self.ingresar(a,b)
ventana = tk.Tk()
main = Ventana(ventana)
ventana.mainloop()
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar