So, i'm trying to create a program that creates a random number (using randint) to an entry field, when a button is pressed. But I can't figure out how to do it.
import tkinter
from tkinter import Entry, END, E
from random import randint
root = tkinter.Tk()
root.title('Number Generator')
e = Entry(root, font=("LEMON MILK Bold", 24), width=15, borderwidth=10)
e.grid(row=0, column=0, columnspan=3, padx=50, pady=50)
e.delete(0, END)
e.insert(0, number)
number=print(randint(0, 100))
#Definitions
def button_generate():
print(number)
#Buttons
button_generate = tkinter.Button(root, text="Random number", font=("LEMON MILK Bold", 24), padx=10,
pady=10, command=button_generate)
button_exit = tkinter.Button(root, text="Exit", font=("LEMON MILK Bold", 14), padx=5, pady=5,
command=root.quit)
#Grid
button_generate.grid(row=1, column=0, columnspan=3)
button_exit.grid(row=2, column=2, sticky=E)
root.mainloop()
So basically how can I make the random number print into the entry field? (Right now it is not printing anywhere due to my bad code)
Please check this out
from tkinter import *
from random import randint
def set_text():
number=randint(0, 100)
e.delete(0,END)
e.insert(0,number)
print(number)
win = Tk()
e = Entry(win,width=10)
e.pack()
b1 = Button(win,text="Gen",command=set_text)
b1.pack()
win.mainloop()
Related
A user has got 10 buttons and the diamond is hidden in one by using random function.
User has got three attempts to find out in which box is the diamond.
from tkinter import *
import tkinter.messagebox as msgBox
window = Tk()
window.title("Find the diamond")
window.configure(bg="gray", pady=100)
window.geometry("720x420")
window.resizable(0,0)
number_of_guesses = 0
random_box_number = (1,10)
HideTheDiamond = Button(window, text = "Hide the diamond", bg ="green")
import random
import sys
Button1 = Button(window, text = "1", width=10,height=3, bg="yellow")
Button2 = Button(window, text = "2",width=10,height=3, bg="blue")
Button3 = Button(window, text = "3",width=10,height=3, bg="dark red")
Button4 = Button(window, text = "4",width=10,height=3, bg="green")
Button5 = Button(window, text = "5", width=10,height=3, bg="red")
Button6 = Button(window, text = "6",width=10,height=3, bg="purple")
Button7 = Button(window, text = "7",width=10,height=3, bg="brown")
Button8 = Button(window, text = "8",width=10,height=3, bg="orange")
Button9 = Button(window, text = "9",width=10,height=3, bg="dark blue")
Button10 = Button(window, text = "10", width=10,height=3, bg="pink")
label11=Label(window,text="Click the Hid the diamond button to start the game. \n Then,
click on the box where you think the diamond is.\n You have three guesses to find
it.",bg="gray")
Button1.grid(row=1, column=1, padx=10, pady=10,sticky=W)
Button2.grid(row=1, column=2,padx=10, pady=10, sticky=W)
Button3.grid(row=1, column=3,padx=10, pady=10, sticky=W)
Button4.grid(row=1, column=4, padx=10, pady=10, sticky=W)
Button5.grid(row=1, column=5, padx=10, pady=10, sticky=W)
Button6.grid(row=2, column=1, padx=10, pady=10, sticky=W)
Button7.grid(row=2, column=2, padx=10, pady=10, sticky=W)
Button8.grid(row=2, column=3, padx=10, pady=10, sticky=W)
Button9.grid(row=2, column=4,padx=10, pady=10, sticky=W)
Button10.grid(row=2, column=5,padx=10, pady=10, sticky=W)
label11.grid(row =5, column =2, sticky=W)
HideTheDiamond.grid(row=5, column =5)
HideTheDiamond.configure(state=NORMAL)
label11.configure(width=40, height=10)
Button1.configure(state=DISABLED)
Button2.configure(state=DISABLED)
Button3.configure(state=DISABLED)
Button4.configure(state=DISABLED)
Button5.configure(state=DISABLED)
Button6.configure(state=DISABLED)
Button7.configure(state=DISABLED)
Button8.configure(state=DISABLED)
Button9.configure(state=DISABLED)
Button10.configure(state=DISABLED)
import random
#staticmethod
def random(self):
global_number_of_guesses = 0
global_box_number = random.randint(1, 10)
return global_box_number
HideTheDiamond.configure(command=random)
def connect():
Button1.configure(state=NORMAL)
Button2.configure(state=NORMAL)
Button3.configure(state=NORMAL)
Button4.configure(state=NORMAL)
Button5.configure(state=NORMAL)
Button6.configure(state=NORMAL)
Button7.configure(state=NORMAL)
Button8.configure(state=NORMAL)
Button9.configure(state=NORMAL)
Button10.configure(state=NORMAL)
HideTheDiamond.configure(state=DISABLED)
connnecting box_number to button
def box_number(self):
self.box_number = box_number
return box_number()
HideTheDiamond.configure(command=connect)
def checkGuess():
if box_number == random:
number_of_guesses += 1
print("you have guessed the number")
else:
print("wrong.two more attemps left")
if number_of_guesses == 3:
print("you lost")
checkGuess()
window.mainloop()
global_number_of_guesses equals to three
global_box_number is random number or box system uses to hide the diamond in
box_number is the box user clicks on in order to find out if diamond is in there
from tkinter import *
import random
window = Tk()
global guesses
guesses = 0
def start_game():
global location
location = random.randrange(1, 11, 1)
def clicked(button_number):
global guesses, location
guesses += 1
if guesses > 3:
# tell the player he lost here in any way you like
print("you lost")
else:
if button_number == location:
# Tell the player he won
print("You won")
else:
# The player guessed wrong but can try again...
print("Not correct, sorry")
for i in range(1, 11):
Button(window, command=lambda i=i: clicked(i).grid(row=i//5, column=i%5, sticky='nswe'))
b11 = Button(window, text="Click here to start the game", command=start_game)
b11.grid()
window.mainloop()
Not the best solution but it should work.
I hope this provides some help, let me know if there are any problems with this.
all of my code is ready except the placement of the Quit button. I want to move to the Calculate button as much as possible, even like sticked to each other. Could you help in adjusting so the buttons will stick to each other?
from tkinter import *
window = Tk()
class MainGUI:
def __init__(self):
Label(window, text="Enter the property value: $").grid(row=1,column=1, sticky=W)
Label(window, text="Assessment Value").grid(row=2,column=1, sticky=W)
Label(window, text="Property Tax").grid(row=3,column=1, sticky=W)
self.propertyValue = StringVar()
self.assessmentValue = StringVar()
self.propertyTax = StringVar()
Entry(window, textvariable=self.propertyValue,justify=RIGHT).grid(row=1, column=2)
Button(window, text="Calculate",
command=self.calculate).grid(row=6, column=1, sticky=E)
Button(window, text="Quit",
command=self.close_window).grid(row=6, column=2, sticky=E)
Label(window, textvariable=
self.assessmentValue).grid(row=2, column=2, sticky=E)
Label(window, textvariable=
self.propertyTax).grid(row=3, column=2, sticky=E)
window.mainloop() # Create an event loop
def calculate(self):
self.assessmentValue.set("{0:10.2f}".format(float(self.propertyValue.get()) * 60 / 100))
self.propertyTax.set("{0:10.2f}".format(float(self.propertyValue.get()) * 60 / 10000 * 0.75))
def close_window(self):
window.destroy()
MainGUI()
This is for a python project on GitHub where I'm making a GUI for a Magic 8 Ball simulation. I cant seem to use the .pack() function or my window just loads forever without ever instantiating.
When created
When I click a button the text appears
window = Tk()
window.configure(bg="black")
window.title("Magic 8 Ball")
Label(window, text="Ask the Magic 8 Ball the question on your mind or enter X to exit: ", bg="black", fg="white")\
.grid(row=0, column=0)
# Create entry box to type question
entrybox = Entry(window, width=30, bg="white")
entrybox.grid(row=1, column=0)
# Create output box at below buttons
output = Text(window, bg="white", fg="black", width=40, height=5)
output.grid(row=4, column=0)
# Create 4 button: Ask, Clear, Play Again, Quit
button_frame = Frame(window)
button_frame.configure(bg="black")
button_frame.grid(row=2, column=0)
#button_frame.pack(fill=X, side=BOTTOM)
Button(button_frame, text="Ask", width=10, bg="black", fg="white", command=click).grid(row=2, column=0)
Button(button_frame, text="Clear", width=10, command=clear).grid(row=2, column=1)
Button(button_frame, text="Play Again", width=10,command=repeat).grid(row=3, column=0)
Button(button_frame, text="Quit", width=10, command=close).grid(row=3, column=1)
window.mainloop()
I think we will need more specifications about your OS and python version. I ran it on Python 3.5.0 and Windows 10 with the result:
So I do not think it is an error in your code. I did have to add in all the functions and import you were missing so what I ran ended up looking like:
from tkinter import *
window = Tk()
def click():
print('click')
def clear():
print('clear')
def repeat():
print('repeat')
def close():
print('close')
window.configure(bg="black")
window.title("Magic 8 Ball")
Label(window, text="Ask the Magic 8 Ball the question on your mind or enter X to exit: ", bg="black", fg="white")\
.grid(row=0, column=0)
# Create entry box to type question
entrybox = Entry(window, width=30, bg="white")
entrybox.grid(row=1, column=0)
# Create output box at below buttons
output = Text(window, bg="white", fg="black", width=40, height=5)
output.grid(row=4, column=0)
# Create 4 button: Ask, Clear, Play Again, Quit
button_frame = Frame(window)
button_frame.configure(bg="black")
button_frame.grid(row=2, column=0)
#button_frame.pack(fill=X, side=BOTTOM)
Button(button_frame, text="Ask", width=10, bg="black", fg="white", command=click).grid(row=2, column=0)
Button(button_frame, text="Clear", width=10, command=clear).grid(row=2, column=1)
Button(button_frame, text="Play Again", width=10,command=repeat).grid(row=3, column=0)
Button(button_frame, text="Quit", width=10, command=close).grid(row=3, column=1)
window.mainloop()
I'm trying to create a program in where you put a word in a box, press add, and this word goes to a list, which is also displayed on the right side. When I press the forward button the first thing on the list is deleted. Problem is I can't get the labels to update when I press the buttons / edit the list.
from tkinter import *
root = Tk()
root.title('Speakers List')
root.minsize(800, 600)
speakers = ['none']
spe = speakers[0]
def add():
if spe == 'none':
speakers.insert(0, [s])
e.delete(0, END)
spe.config(text=speakers[0])
else:
speakers[-2] = [s]
e.delete(0, END)
spe.config(text=speakers[0])
return
def forward():
if len(speakers) is 0:
return
else:
del speakers[0]
spe.config(text=speakers[0])
return
entry = StringVar()
e = Entry(root, width=30, font=("Arial", 20), textvariable=entry)
e.grid(row=0, sticky=W)
s = e.get()
button1 = Button(root, padx=10, pady=10, bd=5, text='Add', fg='black', command=add)
button1.grid(row=0, column=1)
button2 = Button(root, padx=10, pady=10, bd=5, text='Next', fg='black', command=forward)
button2.grid(row=1, column=1)
n = Label(root, font=("Arial", 35), bd=2, text=spe)
n.grid(row=1, sticky=W)
listdisplay = Label(root, font=('Arial', 20), text=speakers)
listdisplay.grid(row=0, column=10)
root.mainloop()
Is this the sort of thing you were looking for ?
from tkinter import *
root = Tk()
root.title('Speakers List')
root.minsize(800, 600)
speakers = ['50']
spe = speakers[0]
def add():
entry=e.get()
speakers.append(entry)
listdisplay.config(text=speakers)
return
def forward():
if len(speakers) is 0:
return
else:
del speakers[0]
listdisplay.config(text=speakers)
spe=speakers[0]
n.config(text=spe)
return
entry = StringVar()
e = Entry(root, width=30, font=("Arial", 20), textvariable=entry)
e.grid(row=0, sticky=W)
s = e.get()
button1 = Button(root, padx=10, pady=10, bd=5, text='Add', fg='black',command=add)
button1.grid(row=0, column=1)
button2 = Button(root, padx=10, pady=10, bd=5, text='Next', fg='black',command=forward)
button2.grid(row=1, column=1)
n = Label(root, font=("Arial", 35), bd=2, text=spe)
n.grid(row=1, sticky=W)
listdisplay = Label(root, font=('Arial', 20), text=speakers)
listdisplay.grid(row=0, column=10)
root.mainloop()
If so:
You create a list and then you use the append function to add an item to it. The rest was pretty much right.
Im new to Tkinter and am trying to build a simple GUI using grid manager which upon the push of button1, button2 appears along with an adjacent entry box. If you then press button2 the entry box and button2 dissapear. Below is a slice from the GUI code, the button dissapears but the entry box does not:
import Tkinter
from Tkinter import *
master = Tk()
CreateTestButton = Button(master, text="Create Test", command = CreateTest, fg="red", bg="white", font="Helvetica 10 bold")
CreateTestButton.grid(column=7, row=1)
def CreateTest():
TestEntry = Entry(master, text="", width = 100).grid(row=4,columnspan=6)
Label(self, text="Enter Test Name:").grid(row=3, column=0)
SaveTestButton = Button(master, text="Save to database", command=saveTest, fg="green", bg="white", font="Helvetica 10 bold")
SaveTestButton.grid(row=4, column=5)
def saveTest():
SaveTestButton.grid_remove()
TestEntry.grid_remove() #ERROR
mainloop()
How is one to remove entry boxes using grid manager in Tkinter? And other widgets for that matter I will also be needing to remove a list box, labels and widgets uppon a button click or event.
Regards,
Daniel
grid return nothing; By executing TestEntry = Entry(..).grid(...), TestEntry become None instead of Entry object.
Replace following line:
TestEntry = Entry(self, text="", width = 100).grid(row=4,columnspan=6)
with:
TestEntry = Entry(self, text="", width = 100)
TestEntry.grid(row=4,columnspan=6)
Complete code
from Tkinter import *
def CreateTest():
def saveTest():
SaveTestButton.grid_remove()
TestEntry.grid_remove() #ERROR
TestEntry = Entry(master, text="", width = 100)
TestEntry.grid(row=4,columnspan=6)
Label(master, text="Enter Test Name:").grid(row=3, column=0)
SaveTestButton = Button(master, text="Save to database", command=saveTest, fg="green", bg="white", font="Helvetica 10 bold")
SaveTestButton.grid(row=4, column=5)
master = Tk()
CreateTestButton = Button(master, text="Create Test", command = CreateTest, fg="red", bg="white", font="Helvetica 10 bold")
CreateTestButton.grid(column=7, row=1)
mainloop()