Clicker game in python not working [duplicate] - python

This question already has answers here:
Why is my Button's command executed immediately when I create the Button, and not when I click it? [duplicate]
(5 answers)
Closed 4 years ago.
I have the code off a clicker below and I'm trying to solve how to show to the number of clicks on the window in tkinter. I'm new to python. If I click on the button the number of clicks remains the same. I don't know if the text doesn't updates or if the increase() function doesn't work. Please help me solve this.
from tkinter import *
clicks = 0
def increase(clicks):
clicks += 1
root = Tk()
root.geometry('500x300')
label=Label(text="Clicks:")
show=Label(text=clicks)
btc = Button(text="Click me", command=increase(clicks))
label.pack()
show.pack()
btc.pack()
root.mainloop()

You need to set the label to have a variable of a particular kind called an IntVar()
Then use .set() and .get() to change the variable value and get its value. When it is changed then the label is automatically.
I suggest you have a look at this link.
Working Code:
from tkinter import *
def increase():
clicks.set(clicks.get() + 1)
root = Tk()
root.geometry('500x300')
label=Label(root, text="Clicks:")
clicks = IntVar()
show=Label(root, textvariable=clicks)
btc = Button(root, text="Click me", command=increase)
label.pack()
show.pack()
btc.pack()
root.mainloop()

Related

Printing out information with python? [duplicate]

This question already has answers here:
send the output of print() to tkinter Text widget
(3 answers)
Closed yesterday.
I'm trying to print out information into this window and not in the console. How do I do this?
from tkinter import *
print("Hello")
gui = Tk(className='StreetView Map')
# set window size
gui.geometry("500x200")
gui.mainloop()
I tried using the print function but nothing showed up in the window.
You can create a Label.
Label(gui, text="Hello").pack()
You need to Create a Text widget where you can insert the test on GUI on Tkinter.
Given your code it would have to be done like this.
from tkinter import *
gui = Tk(className='StreetView Map')
# set window size
gui.geometry("500x200")
T = Text(root, height = 5, width = 52)
T.pack()
#insert the string in Text Widget
T.insert(tk.END, "hello")
gui.mainloop()

How can I detect if a user click a tkinter button? [duplicate]

This question already has answers here:
How to handle a click button event in python module tkinter
(2 answers)
Closed 2 years ago.
Here is my code!
///
from tkinter import *
import os
import pygame
os.system(‘clear’)
#Window Setup
root = Tk()
root.title(‘cottontail’)
root.geometry(‘800x600’)
frame = Frame(root)
title_screen = Label(root, text = “Choose your rabbits name!”)
title_screen.pack()
name = Text(root, width=10 , height=3)
name.pack()
confirm_name = Button(root, text= “Conirm?”, width = 5, height=3)
root.mainloop()
///
My objective is to take the input the user puts in the text box to make a label in a pygame window with that name. I figured that a button would be an easy way to confirm the name and open the pygame screen. If this makes any sense to you it would really be appreciated if you could help me. Hope you have a good night!
Thanks in advance!
I'm not familiar with tkinter as I'm a PyQt dev, but I can help with your question as the logic behind both of them are same.
You can make a function that is called by the button whenever its clicked, and then, inside that function definition, do whatever you want.
Here's the website that explains this in detail -
https://www.delftstack.com/howto/python-tkinter/how-to-get-the-input-from-tkinter-text-box/

How to create a window with Python [duplicate]

This question already has answers here:
Easiest way to develop simple GUI in Python [closed]
(7 answers)
Closed 6 years ago.
I am learning to program with Python 3.6, and would like to ask for help on building a window. It would be greatly appreciated if someone would please show me the basics, like how to make the window, how to make buttons do things, input and output boxes, and stuff like that. I would prefer not to use pyQT or something like that.
The tkinter module is probably the most common Python GUI method.
To make a button:
from tkinter import *
tk = Tk()
btn = Button(tk, text="a clickable button", command())
btn.pack()
To make an input box:
from tkinter import *
tk = Tk()
inputBox = Entry(tk, bd=5)
#to read your box
inputBox.get()
To make a label:
from tkinter import *
tk = Tk()
label = Label(tk, text="your text here")

How to make default text in entry that disappears [duplicate]

This question already has answers here:
Entry box text clear when pressed Tkinter
(2 answers)
Closed 6 years ago.
For instance:
from Tkinter import *
root = Tk()
e1 = Entry(root)
e1.insert(END, "ex. new file") #would like to make this text disappear when clicked
e1.grid(row=0, column=0)
root.mainloop()
Where the text "ex. newfile" disappears when clicked upon, leaving a blank entry field.
Create a boolean flag that monitors if the entry has been accessed; set it to False,
Bind "<Button-1>" to a function that clears the entry if it has not been accessed yet, and changes the flag to True.
Added
def delete_text(event):
if default_text:
e1.delete(0, END)
default_text = False
default_text = True
e1.bind("<Button-1>", delete_text)
Thanks to DYZ and effbot

How to open a new window by clicking a button using Tkinter in python? [duplicate]

This question already has answers here:
How can I open a new window when the user clicks the button?
(2 answers)
Closed 6 years ago.
I want to make a gui application in python for that I was making this type of code.
I have already tired many codes but i was not able to make it up to the requirement.
What's stopping you from doing it, please refer the original post here. But basic code:
import Tkinter as tk
def create_window():
window = tk.Toplevel(root)
root = tk.Tk()
b = tk.Button(root, text="Create new window", command=create_window)
b.pack()
root.mainloop()

Categories

Resources