I would like to press a button and have it print the location of the cursor X/Y coordinates as well as the content of the button in tkinter.
I'm new to python and I haven't found any way to do this. Can anyone help me finding the right start on this issue?
You can use the bind the button click event to print the location clicked, here's an example of how you can do that.
import tkinter as tk
def print_location(event):
print(event.x, event.y)
root = tk.Tk()
button = tk.Button(root, text='click me')
button.bind("<Button-1>", print_location)
button.pack()
root.mainloop()
Note: using external modules such as pyautogui is not recommended and unnecessary.
Related
import tkinter
win=tkinter.Tk()
win.configure(background='grey')
k=False
def g():
k=True
v=tkinter.Button(win, text='click', command=g)
v.pack()
while k==True:
win.configure(background='black')
win.mainloop()
There's no reason why that while loop would run after the button is clicked, since (as you know) your program is run "from top to bottom", and control remains in win.mainloop() until the window is closed. (You can find that out by adding print("bye!") after that call.)
You might want to just directly call .configure(). (I gave the button some padding here so you can see the background change; otherwise the button may take up the entirety of the window and you won't see a change.)
import tkinter
win = tkinter.Tk()
def change_color():
win.configure(background='black')
button = tkinter.Button(win, text='click', command=change_color)
button.pack(padx=10, pady=10)
win.mainloop()
0 iq Question incoming, i wanna know is there a way i can show and hide a tkinter gui with insert key, ive searched online but wasnt able to find an answer, for example like csgo menus.
Thank you.
Please make sure to always include what you have tried and your research. Follow these guidelines to create a minimal reproducible example.
What you are trying to accomplish can be done with this Tkinter template:
from tkinter import *
root = Tk()
# Open new window
def launch():
global second
second = Toplevel()
second.title("Child Window")
second.geometry("400x400")
# Show the window
def show():
if event.keysym == "Insert"
second.deiconify()
# Hide the window
def hide():
if event.keysym == "Insert"
second.withdraw()
# Add Buttons
Button(root, text="launch Window", command=launch).pack(pady=10)
Button(root, text="Show", command=show).pack(pady=10)
Button(root, text="Hide", command=hide).pack(pady=10)
root.mainloop()
This is my code:
import tkinter as tk
window=tk.Tk()
window.title("Click Me")
click=tk.Button(window, text="Click Me", highlightbackground="blue", activebackground="red")
click.place(x=200,y=300)
window.mainloop()
It doesn't return any error to me. But, when I click on the button, nothing happens.
Please reply to me as soon as you can.
I don't think the Mac allows you to change the background or activebackground of buttons. Apple wants to control the look of standard controls.
I have added two things to make this work:
a call back function helloCallback
pack the button properly so that it is visable.
here is the code:
import tkinter as tk
window=tk.Tk()
window.title("Click Me")
# a test function to execute when the button is clicked
def helloCallback():
print('hi there, it works')
return
click=tk.Button(window, text="Click Me", highlightbackground="blue", activebackground="red", command=helloCallback)
# pack the button
click.pack()
window.mainloop()
result: when the button is pressed, it prints hi there, it works to the console.
Hi guys im trying to make a subprogram that waits for a button to be pressed in order to continue, in othe words: a loop that has a condidtinal, beeing the conditianal having a button pressed, and I have no idea of what can i use to do that.
Here is an easy example of what others have said, this function waits until you hit the button, when it will run the function written:
import tkinter as tk
root = tk.Tk()
root.title('waiting for button click...')
def carry_on:
# do what ever
button = tk.Button(text = 'click to continue', command = carry_on)
button.pack()
root.mainloop()
if you have a more specific question please add more information :)
I want to keep a menu cascade open, after a command button within the cascade is clicked. So it basically only closes when the user clicks anywhere else (like it would normally too). Can't seem to find a proper option or a method to open said menu in the callback. The invoke() function only works on buttons wihtin the cascade right? How would you go about that?
Yes, I know this was asked a long time ago, but I was curious if there was any way to accomplish this with tkinter, so I fiddled about for a while and figured out how to do it. I was unable to come up with a way to properly place the persistent menu where it was when it originally opened, but I have managed to make it persist in any location you request (I use upper-left corner of root window). And yes, I know this isn't a nice proper class based implementation, but I was just going for as simple a test as I could write without obscuring it with too many extraneous details.
try:
from tkinter import *
from tkinter.ttk import *
except:
from Tkinter import *
from ttk import *
root = Tk()
var = StringVar()
def menu_click(menu, item):
global root
var.set(item)
menu.post(root.winfo_rootx(), root.winfo_rooty())
root.option_add('*tearOff', False) # remove tearoff from all menus
Label(root, textvariable=var).pack() # just to give menu clicks some feedback
root.geometry('400x300')
menubar = Menu(root)
root['menu'] = menubar
menu_test = Menu(menubar)
menubar.add_cascade(menu=menu_test, label='Test')
menu_test.add_command(label='One', command=lambda: menu_click(menu_test, 'One'))
menu_test.add_command(label='Two', command=lambda: menu_click(menu_test, 'Two'))
menu_test.add_command(label='Three', command=lambda: menu_click(menu_test, 'Three'))
menu_cas = Menu(menu_test)
menu_test.add_cascade(menu=menu_cas, label='Four')
menu_cas.add_command(label='One', command=lambda: menu_click(menu_cas, 'Fourty One'))
menu_cas.add_command(label='Two', command=lambda: menu_click(menu_cas, 'Fourty Two'))
menu_cas.add_command(label='Three', command=lambda: menu_click(menu_cas, 'Fourty Three'))
root.mainloop()