Noob Alert!!!
Hello, I just started my journey with through python a couple of days ago, so my question will most likely be extremely simple to answer. Basically I have a random number gen. from 1 to 10, this is activated when the "test button is pressed on the windows that pops up. As you can see from the image below the random number appears in the output console on the bottom of the screen, in this case it was a 9. So here's the question, How can I make the random number on the GUI? so when the button is pressed a random number appears on the same window as the button.
https://i.stack.imgur.com/hWd3i.png
Any help is appreciated!
from tkinter import *
root = Tk()
root.geometry("300x300")
root.title("test it is")
root.grid()
def randnum(event):
import random
value =random.randint(1,10)
print(value)
button_1 = Button(root, text="test")
button_1.bind("<Button-1>",randnum)
button_1.pack()
root.mainloop()
from tkinter import *
root = Tk()
root.geometry("300x300")
root.title("test it is")
root.grid()
def randnum(event):
import random
value =random.randint(1,10)
print(value)
updateDisplay(value)
def updateDisplay(myString):
displayVariable.set(myString)
button_1 = Button(root, text="test")
button_1.bind("<Button-1>",randnum)
button_1.pack()
displayVariable = StringVar()
displayLabel = Label(root, textvariable=displayVariable)
displayLabel.pack()
root.mainloop()
Here is what it looks like.You have to create a Label with a Button, whose value will get updated when you click on button.
import tkinter as tk
from random import randint
win = tk.Tk()
def test_button_click():
label_val.set(randint(1, 10))
my_button = tk.Button(win, text='Test Button',
command=test_button_click)
my_button.grid(column=0, row=0)
label_val = tk.IntVar()
my_Label = tk.Label(win, textvariable=label_val)
my_Label.grid(column=1, row=0)
win.mainloop()
This will achieve what you are requesting -- create a tk window, add a button and label, use the callback test_button_click to set the labels int var when the button is clicked.
Related
I'd like to print output value from spinbox, after clicking enter. I don't know why it is printing always value "from_" (so 1). What is the problem? Thank you.
import tkinter as tk
gui = tk.Tk()
gui.geometry("300x390")
gui.title("Test")
var = tk.IntVar()
tk.Spinbox(
gui,
textvariable=var,
from_=1,
to=10).pack()
a = var.get()
def print_test():
print(a)
enter = tk.Button(gui, text="Enter", command=print_test).pack(pady=15)
gui.mainloop()
I am trying to create a tkinter application where every time you press a button an image moves down a certain number of pixels. For example, the first time it is placed at y=30 and then the next time the button is pressed it is placed at y=60 etc. Is there any way to do this? I do not want to use the pack() method as I need to place the image in a specific location on the screen using x and y coordinates.
import calendar
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
root.geometry('800x800')
def display():
box_image = tk.PhotoImage(file='apple.png')
panel2 = tk.Label(root, image=box_image, bg='#f7f6f6')
panel2.image = box_image
panel2.place(x=30, y=30 + 30) #i was thinking about doing something like adding 30 each time but this didn't work
button = tk.Button(root, text="click me", command=display)
button.place(x=0, y=0)
root.mainloop()
do the following :
import calendar
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
root.geometry('800x800')
x = 30
y = 30
box_image = tk.PhotoImage(file=r'apple.png')
def display():
global x , y
panel2 = tk.Label(root, image=box_image, bg='#f7f6f6')
panel2.place(x=x+30, y=y+30)
x = x+30
y = y+30
button = tk.Button(root, text="click me", command=display)
button.place(x=0, y=0)
root.mainloop()
What my code does currently is create a label with the text "Hello" every time I press the button that says "Say hello"
What I'm trying to figure out is how to create a button that clears off all of the labels off the screen, however I'm completely clueless.
How do I create a button that clears all of the labels off of the screen?
My code is down below.
import tkinter as tk
import time
root = tk.Tk()
root.geometry("700x500")
h = "Hello"
def CreateLabel():
helloLabel = tk.Label(root, text=h)
helloLabel.pack()
labelButton = tk.Button(root, text="Say hello", command=CreateLabel)
labelButton.pack()
root.mainloop()
I'm beginner but try this:
import tkinter as tk
import time
root = tk.Tk()
root.geometry("700x500")
h = "Hello"
liste = []
def CreateLabel():
helloLabel = tk.Label(root, text=h)
helloLabel.pack()
liste.append(helloLabel)
def DelLabel():
for i in range(len(liste)):
liste[i].destroy()
liste.clear()
labelButton = tk.Button(root, text="Say hello", command=CreateLabel)
labelButton.pack()
labelButton = tk.Button(root, text="del hello", command=DelLabel)
labelButton.pack()
root.mainloop()
I've started learning Tkinter on Python few weeks ago and wanted to create a Guess Game. But unfortunately I ran into a problem, with this code the text for the rules ( in the function Rules; text='Here are the rules... ) doesn't appear on the window ( rule_window).
window = Tk()
window.title("Guessing Game")
welcome = Label(window,text="Welcome To The Guessing Game!",background="black",foreground="white")
welcome.grid(row=0,column=0,columnspan=3)
def Rules():
rule_window = Tk()
rule_window = rule_window.title("The Rules")
the_rules = Label(rule_window, text='Here are the rules...', foreground="black")
the_rules.grid(row=0,column=0,columnspan=3)
rule_window.mainloop()
rules = Button(window,text="Rules",command=Rules)
rules.grid(row=1,column=0,columnspan=1)
window.mainloop()
Does anyone know how to solve this problem?
In your code you reset whatever rule_window is to a string (in this line: rule_window = rule_window.title(...))
Try this:
from import tkinter *
window = Tk()
window.title("Guessing Game")
welcome = Label(window, text="Welcome To The Guessing Game!", background="black", foreground="white")
welcome.grid(row=0, column=0, columnspan=3)
def Rules():
rule_window = Toplevel(window)
rule_window.title("The Rules")
the_rules = Label(rule_window, text="Here are the rules...", foreground="black")
the_rules.grid(row=0, column=0, columnspan=3)
rules = Button(window, text="Rules", command=Rules)
rules.grid(row=1, column=0, columnspan=1)
window.mainloop()
When you want to have 2 windows that are responsive at the same time you can use tkinter.Toplevel().
In your code, you have initialized the new instances of the Tkinter frame so, instead of you can create a top-level Window. What TopLevel Window does, generally creates a popup window kind of thing in the application. You can also trigger the Tkinter button to open the new window.
from tkinter import *
from tkinter import ttk
#Create an instance of tkinter window
root= Tk()
root.geometry("600x450")
#Define a function
def open_new():
#Create a TopLevel window
new_win= Toplevel(root)
new_win.title("My New Window")
#Set the geometry
new_win.geometry("600x250")
Label(new_win, text="Hello There!",font=('Georgia 15 bold')).pack(pady=30)
#Create a Button in main Window
btn= ttk.Button(root, text="New Window",command=open_new)
btn.pack()
root.mainloop()
When I click a button, it should write text under it and the text should disappear after few seconds.
I don't know how to code that. What I have tried so far:
from tkinter import *
import time
window = Tk()
window.title("Button")
window.geometry("500x300")
def buttonclick():
tex = Label(text="You clicked the button")
tex.pack()
time.sleep(5)
tex.destroy()
but = Button(text="Click me!", command=buttonclick)
but.pack()
window.mainloop()
You can use .after() method to destroy the label after fixed period of time.
The following example will delete the label after 3 seconds:
from tkinter import *
import time
window = Tk()
window.title("Button")
window.geometry("500x300")
def buttonclick():
tex = Label(text="You clicked the button")
tex.pack()
tex.after(3000, tex.destroy)
but = Button(text="Click me!", command=buttonclick)
but.pack()
window.mainloop()
Output:
Your code looks correct for the most part. The reason why it doesn't appear to be working is that there is nothing telling the window to update after adding the text. A simple fix would be to add window.update() when you create the label.
The Code should look like:
from tkinter import *
import time
window = Tk()
window.title("Button")
window.geometry("500x300")
def buttonclick():
tex = Label(text="You clicked the button")
tex.pack()
window.update()
time.sleep(5)
tex.destroy()
window.update()
but = Button(text="Click me!", command=buttonclick)
but.pack()
window.mainloop()