Python tkinter Button not responding - python

TKINTER GRAPHICS
from tkinter import *
tk = Tk()
btn = Button(tk, text="Click Me") #Makes a useless button
btn.pack() #Shows the button
import turtle #Makes the graphics work
t = turtle.Pen()
def hello(): #Makes button not
print('hello here')
btn = Button(tk, text="Click Me", command=hello)
The program SHOULD say hello there when I click the button, but I can't click the button because it won't respond.

As noted in the comments, you appear to create the same button twice, once where it's not connected to a function but packed, and once where it's connected to a function but not packed. If you combine the two, you'll get a working button.
But let's jump in and fix another problem before it begins -- you invoked:
t = turtle.Pen()
No, not when you're working within tkinter. If you're working with turtle standalone, it's Turtle/Pen and Screen, but if you're working within tkinter, it's RawTurtle/RawPen and TurtleScreen. Otherwise you end up with extra windows and potential root conflicts.
Combining all the above together, adding a scrolled canvas for the turtle to play on, and changing the print() to the console to be a turtle.write() to the scrolled canvas, we get:
import tkinter as tk
import turtle # Makes the graphics work
def hello(): # Makes button not
turtle.write('hello there', align='center', font=('Arial', 18, 'normal'))
root = tk.Tk()
button = tk.Button(root, text="Click Me", command=hello) # Makes a button
button.pack() # Shows the button
canvas = turtle.ScrolledCanvas(root)
canvas.pack(side=tk.LEFT)
screen = turtle.TurtleScreen(canvas)
turtle = turtle.RawPen(screen, visible=False)
screen.mainloop()

Related

Make Button in Python Tkinter Stop Moving when Pressed

So I am working on making an application inside of Tkinter and I am not that experienced in it, but I noticed that when you press a button it moves the text inside down and to the right just a couple pixels and I can't figure out how to make it not do that. Any help finding what parameter I need to change would be greatly appreciated.
I tried finding any docs on the subject but whenever I looked it up I would only get things that pertained to how to place a button on the window or in a frame/canvas with place, grid, pack.
will you can change the relief value to be SUNKEN and adjust the border values like this
Button(root, text = 'Click me !', borderwidth=5, relief=SUNKEN)
the button will look a little weird
or you can use widget other than a button just like this answer (https://stackoverflow.com/a/10674950/20411925)
import tkinter
class main:
def __init__(self,root):
# make a label with some space around the text
self.lbl1 = tkinter.Label(root,
width = 16, height = 4,
text = "Foobar")
self.lbl1.pack()
# Call a function when lbl1 is clicked
# <Button-1> means a left mouse button click
self.lbl1.bind("<Button-1>", self.yadda)
self.lbl1.bind("<Enter>", self.green)
self.lbl1.bind("<Leave>", self.red)
def yadda(self, event):
self.lbl1.config(text="Clicked!")
def green(self, event):
self.lbl1.config(bg="green")
def red(self,event):
self.lbl1.config(bg="red")
if __name__ == "__main__":
root = tkinter.Tk()
main(root)
root.mainloop()

flashing button that stops flashing when pressed in Tkinter, without using flash()

I am new to tkinter, I am trying to make a button that flashes green and silver until it is pressed, at which point it reverts to silver. I followed the code from this website, flash button example, it seemed to be closest to what I was trying to do.
%reset -f
import tkinter as tk
root = tk.Tk()
def stop_flash():
print('stop_flash')
root.after_cancel(flasher2)
root.after_cancel(flasher1)
button = tk.Button(root, text="Hello", command=stop_flash, background='silver', activebackground='red')
button.pack()
def flash():
button.configure(background = 'green')
flasher1 = root.after(500, lambda: button.configure(background = 'silver'))
flasher2 = root.after(1000, flash)
flasher1 = root.after(500, lambda: button.configure(background = 'silver'))
flasher2 = root.after(1000, flash)
root.mainloop()
I got the button to flash but I don't understand why it won't stop. I have tried making a separate switch button so I would only need to use 1 after() function but it gets even messier. Any help here would be greatly appreciated!!!!!

How do I save where I was even if I have escaped the program in tkinter python?

I have been recently trying to make a program that it is saved even if you quit the program
The code
import tkinter as tk
root = tk.Tk()
def disabled():
button["state"] = "disabled"
button = tk.Button(root, text="Click me", command=disabled) # when you click the command will disable it
button["state"] = "normal" # first
button.pack()
root.mainloop()
I want from the button to be disabled even if I have escaped

Tkinter crashes when clicking a button and remains pressed

I have a simple program with start and exit buttons. The start button makes a notification using win10toast, but the button remains visibly pressed down and the window becomes unresponsive. The exit button works fine before the start button is pressed. Here's my code:
from tkinter import *
from win10toast import ToastNotifier
root = Tk()
def exit_p():
exit()
def new():
hr.show_toast("New", "Alert")
return
#creates a label widget
myLabel1 = Label(root, text="Full Moon Notification!")
myLabel2 = Label(root, text="Here you can start and exit the program")
button1 = Button(root, text="Start",padx=50,command=new).grid(row=3,column=0)
button2 = Button(root, text="Exit", padx=50,command=exit_p).grid(row=4,column=0)
#puts the widget on the screen
myLabel1.grid(row=0,column=0)
myLabel2.grid(row=1,column=0)
#loop to keep program running
root.mainloop()
The issue is likely because hr.show_toast("New", "Alert") blocks.
The win10toast library conveniently provides an option threaded=True, so just change that code to
hr.show_toast("New", "Alert", threaded=True)
should make it work.

python tkinter clicking on button to open new window

I've made 3 buttons on my window. I choosed that the main window should have a specific background image and a full screen.
Now there is a problem. I would like to move to a new window (page) (with an other background and other things) by clicking on button 3.
Things i tryd:
from Main.Info.travelhistry import *
I've added this to the main window to open a new python file with the code of the second screen that has to open when clicking on button 3. But I found out that if I do this both windows will open when running main window.
I added root1 = Tk() at the beginning, root1.mainloop() at the end and between them the code for the other window. But this won't work also, its opening 2 windows like above.
Those were all my attempts and i cant figure out a better way. I can but the background would stay the same. But I have to change the background for the new window to a background image i made...
Any idea what im doing wrong?
from tkinter import *
from tkinter.messagebox import showinfo
from Main.Info.travelhistry import *
def clicked1():
bericht = 'Deze functie is uitgeschakeld.'
showinfo(title='popup', message=bericht)
root = Tk()
a = root.wm_attributes('-fullscreen', 1)
#Hoofdmenu achtergrond
C = Canvas(root, bg="blue", height=250, width=300)
filename = PhotoImage(file = "test1.png")
background_label = Label(root, image=filename)
background_label.place(x=0, y=0, relwidth=1, relheight=1)
C.pack()
# Geen OV-chipkaart button
b=Button(master=root, command=clicked1)
photo=PhotoImage(file="button1.png")
b.config(image=photo,width="136",height="53", background='black')
b.place(x=310, y=340)
#Buitenland button
b2=Button(master=root, command=clicked1)
photo1=PhotoImage(file="button2.png")
b2.config(image=photo1,width="136",height="53", background='black')
b2.place(x=490, y=340)
#Reis informatie
b3=Button(master=root)
photo2=PhotoImage(file="button3.png")
b3.config(image=photo2,width="136",height="53", background='black')
b3.place(x=680, y=340)
root.mainloop()
root2.mainloop()
You shouldn't call more than one Tk() window.
Instead, tkinter has another widget called Toplevel which can be used to generate a new window.
See below for an example:
from tkinter import *
root = Tk()
def command():
Toplevel(root)
button = Button(root, text="New Window", command=command)
button.pack()
root.mainloop()
This one opens new window that you can edit.
from tkinter import *
Window = Tk()
def Open():
New_Window = Tk()
#You can edit here.
New_Window.mainloop()
Btn1 = Button(text="Open", command=Open)
Bt1n.pack()
Window.mainloop()

Categories