How can I make that in label generate some numbers for example:
some text(e.g.Input): #here generate numbers#
Input and Output are text in label
I want to make that number are generated every second in the label
To wait one second between each random number, use the after method and change the text in the scheduled function:
from tkinter import Tk, Label
import random
root = Tk()
label = Label(root)
label.pack()
def replace_text():
label.config(text=str(random.random()))
root.after(1000, replace_text)
replace_text()
root.mainloop()
Is this what you're looking for?
Every time you press the button, a new random number is displayed.
import tkinter as tk
import random
class Window:
def __init__(self, master):
self.frame = tk.Frame(master)
self.text = tk.StringVar()
self.text.set(random.randint(1, 10))
self.ranNumLabel = tk.Label(self.frame, textvariable = self.text)
self.genButton = tk.Button(self.frame, text = 'Generate Random Number', command = self.genRanNum)
self.ranNumLabel.grid(row = 0)
self.genButton.grid(row = 1)
self.frame.grid()
def genRanNum(self):
self.text.set(random.randint(1, 10))
# when text is updated, the Label associated with it also updated
def main():
root = tk.Tk(className = ' Random Number Generator')
app = Window(root)
root.mainloop()
if __name__ == '__main__':
main()
Your question is rather confusing, but here is what I can answer...
To generate a random number use the random.random() function and the label() function in tkinter.
Here is an example:
import random
from tkinter import *
x=random.random()
root = Tk()
w = Label(root, text=x)
w.pack()
root.mainloop()
Related
**I am writing a baseball counting program. I have a label for walks and strikeouts. To the side of those labels I have buttons with the text "+1". I want the buttons to be able to change and print the number of walks and strikeouts every time I click the +1 buttons. I just need some ideas to help get started. Here is my code for clarification: **
import tkinter as tk
from tkinter.constants import COMMAND, X
global walk_counter
walk_counter = 0
global strikeout_counter
strikeout_counter = 0
def main(): # This is the main function
root = tk.Tk()
frame = tk.Frame(root)
frame.master.title("Random Title")
frame.pack(padx=4, pady=3, fill=tk.BOTH, expand=1)
populate_boxes(frame)
root.mainloop()
def populate_boxes(frame):
walks_label = tk.Label(frame, text="BB:")
walks_entry = tk.Label(frame, width=4)
walks_button = tk.Button(frame,text="+1")
strikeouts_label = tk.Label(frame,text="Strikeouts:")
strikeouts_entry = tk.Label(frame,width=4)
strikeouts_button = tk.Button(frame,text="+1")
walks_label.place(x=200,y=500)
walks_entry.place(x=250,y=500)
walks_button.place(x=300,y=500)
strikeouts_label.place(x=400,y=500)
strikeouts_entry.place(x=450,y=500)
strikeouts_button.place(x=500,y=500)
def add_more_walks():
global walk_counter
walk_counter += 1
walks_entry.config(text = walk_counter)
add_more_walks()
main()
My code:
def Click():
global output
output = StringVar()
output = random.randint(1, 6)
global outputL
outputL = Label(root, text = f"The number is... {output}")
outputL.pack()
output = 0
How do I make it hide the label after that?
Use after method and config method
(TIP) Don't create a widgets(Labels, Buttons, etc...) inside a function. Use config method to update your widgets
from tkinter import *
import random
root = Tk()
def Remove_Output():
outputL.pack_forget()
def click():
global output
output = StringVar()
output = random.randint(1, 6)
outputL.config(text=f"The number is... {output}")
outputL.pack()
output = 0
root.after(1000, Remove_Output)
btn = Button(root, text="Click",command=click)
btn.pack()
outputL = Label(root, text = "")
root.mainloop()
Is there a way I can use a Funtion in the Binding-Method, which takes Parameters as a Label and the Text that I want to change of the passed Label ?
from tkinter import *
def changeLabelName(Label, text):
Label.configure(text=text)
def main():
root = Tk()
testLabel = Label(root, text="")
testLabel.pack()
testEntry = Entry()
testEntry.bind('<Return>', lambda: changeLabelName(testLabel, "Hi"))
testEntry.pack()
root.mainloop()
if __name__ == "__main__":
main()
import random
from tkinter import *
root = Tk()
random_number = str(random.randint(1,11))
def myClick():
myLable = Label(root, text=f"{random_number}")
myLable.pack()
rand_button = Button(root, text="Press for a random number",
command=myClick)
rand_button.pack()
root.mainloop()
This should work
import random
from tkinter import *
root = Tk()
random_number = str(random.randint(1, 11))
MyLabel = Label(Tk, text=random_number)
MyLabel.pack()
def myclick():
random_number = str(random.randint(1, 11)) # Assigning random number
MyLabel.config(text=random_number) # Changing text to random number
randButton = Button(root, text="Press for a random number", command=myClick)
randButton.pack()
root.manloop()
The main problem with your code was that you had to make a new random number every time you clicked the button and you had to change the text of the same label, or you would have a bunch of labels stacking on each other.
You're assigning a random number to the random_number variable once, so it never changes.
If you move it to the method it will generate a new one each time.
import random from tkinter import * root = Tk()
def myClick():
random_number = str(random.randint(1,11)) # Generates a new # when the method is called.
myLable = Label(root, text=f"{random_number}") myLable.pack()
rand_button = Button(root, text="Press for a random number", command=myClick)
rand_button.pack()
root.mainloop()
Is it possible to code time HH:MM:SS using text widget?
I know we can do this with label widget. My code deals with text widget and want to display time at any corner on the TKinter window.
If possible how to delete and insert the text using label widgets. Text widget has the following methods default.
delete(startindex [,endindex])
This method deletes a specific character or a range of text.
insert(index [,string]...)
This method inserts strings at the specified index location.
In my code the text has to be deleted and inserted all the time.
thanks.
delete from 1.0 to end.
import datetime
text.delete('1.0', 'end')
text.insert('end', datetime.datetime.now().strftime('%H:%M:%S')) # text.insert('end', label['text'])
label['text'] = datetime.datetime.now().strftime('%H:%M:%S')
Display Entry on bottom right
import datetime
from Tkinter import * # from tkinter import * # Python 3.x
root = Tk()
root.geometry('500x500')
frame = Frame(root)
frame.pack(side=BOTTOM, fill=BOTH)
entry = Entry(frame)
entry.pack(side=RIGHT)
entry.insert(END, datetime.datetime.now().strftime('%H:%M:%S'))
root.mainloop()
def __init__(self):
tk.Tk.__init__(self)
self.geometry("1360x750")
frameLabel = tk.Frame(self)
self.text = tk.Text(frameLabel)
frameLabel.pack(side=BOTTOM, fill=BOTH)
self.text.pack(side=RIGHT)
self.text.insert('end', 'TEST')
self.queue = Queue.Queue()
thread = SerialThread(self.queue)
thread.start()
self.process_serial()
UPDATE2
import datetime
from Tkinter import * # from tkinter import * # Python 3.x
root = Tk()
root.geometry('500x500')
frame = Frame(root)
frame.pack(side=BOTTOM, fill=BOTH)
label = Label(frame)
label.pack(side=RIGHT)
label['text'] = datetime.datetime.now().strftime('%H:%M:%S')
root.mainloop()