How to display text in the window when the cursor is placed over the button.
I have below code, when put cursor over the button "Ok" it has to show text as "Check details filled before pressing Ok".
import Tkinter
class Example(Tkinter.Frame):
def __init__(self, *args, **kwargs):
Tkinter.Frame.__init__(self, *args, **kwargs)
self.l1 = Tkinter.Label(self, text="Enter name")
self.l2 = Tkinter.Label(self, text="", width=40)
self.l1.pack(side="top")
self.l2.pack(side="top", fill="x")
self.b1 = Tkinter.Button(root, text="Ok")
self.b1.bind("<Enter>", self.on_enter)
self.b1.bind("<Leave>", self.on_leave)
self.b1.pack()
def on_enter(self, event):
self.l2.configure(text="Check details filled before pressing Ok")
def on_leave(self, enter):
self.l2.configure(text="")
if __name__ == "__main__":
root = Tkinter.Tk()
Example(root).pack(side="top", fill="both", expand="true")
root.mainloop()
The same code works fine if I write for display text when cursor is placed over label l1. Is there any other way to proceed for displaying when cursor placed on button or any modifications??
If I understand the question correctly, you want to be able to display different messages when the mouse is over different widgets. The simplest solution is to add a custom attribute to each widget object that contains the message, and then get that message from the attribute in the bound function.
For example:
class Example(...):
def __init__(...):
...
self.l1.description = "This is label 1"
self.l2.description = "This is label 2"
self.b1.description = "This is the OK button"
for widget in (self.l1, self.l2, self.b1):
widget.bind("<Enter>", self.on_enter)
widget.bind("<Leave>", self.on_leave)
...
def on_enter(self, event):
description = getattr(event.widget, "description", "")
self.l2.configure(text=description)
As far as I know, Tk doesn't have a built in construct for button ToolTips
In Qt (PyQt), which is another GUI framework, this a a built in feature - for example:
button1 = QtGui.QPushButton("This is button1", self)
button1.setToolTip("You have moused over Button1)
There are some workarounds for adding this type of functionality to Tk but it may take you some time to directly implement them into your program
Essentially you create your own class ToolTip() and a function in your module to add a new ToolTip, addToolTip()
Here are two references for doing this:
ref 1
ref 2
Edit: Note that ref1 here is the same link that is the accepted answer in the question Martineau links to in the comments for this question.
Related
I've written a password generator with Tkinter and have set a messagebox that pops-up when the data for the website is already in the database.
Is there an option that allows me to copy the text from the pop-up? Because these passwords are really long. Or do I need to go into the file where it is saved to copy it?
messagebox.showinfo(title=website, message=f" Email: {email}\nPassword: {password}")
Try something like this:
import tkinter as tk
class Popup:
def __init__(self, title:str="Popup", message:str="", master=None):
if master is None:
# If the caller didn't give us a master, use the default one instead
master = tk._get_default_root()
# Create a toplevel widget
self.root = tk.Toplevel(master)
# A min size so the window doesn't start to look too bad
self.root.minsize(200, 40)
# Stop the user from resizing the window
self.root.resizable(False, False)
# If the user presses the `X` in the titlebar of the window call
# self.destroy()
self.root.protocol("WM_DELETE_WINDOW", self.destroy)
# Set the title of the popup window
self.root.title(title)
# Calculate the needed width/height
width = max(map(len, message.split("\n")))
height = message.count("\n") + 1
# Create the text widget
self.text = tk.Text(self.root, bg="#f0f0ed", height=height,
width=width, highlightthickness=0, bd=0,
selectbackground="orange")
# Add the text to the widget
self.text.insert("end", message)
# Make sure the user can't edit the message
self.text.config(state="disabled")
self.text.pack()
# Create the "Ok" button
self.button = tk.Button(self.root, text="Ok", command=self.destroy)
self.button.pack()
# Please note that you can add an icon/image here. I don't want to
# download an image right now.
...
# Make sure the user isn't able to spawn new popups while this is
# still alive
self.root.grab_set()
# Stop code execution in the function that called us
self.root.mainloop()
def destroy(self) -> None:
# Stop the `.mainloop()` that's inside this class
self.root.quit()
# Destroy the window
self.root.destroy()
def show_popup():
print("Starting popup")
Popup(title="title", message="Message on 1 line", master=root)
print("Ended popup")
print("Starting popup")
Popup(title="title", message="Message\nOn 2 lines", master=root)
print("Ended popup")
root = tk.Tk()
root.geometry("300x300")
button = tk.Button(root, text="Click me", command=show_popup)
button.pack()
root.mainloop()
It's just a simple class that behaves a lot like messagebox.showinfo. You can add an icon if you want. Please note that some of the functionality is missing but it should work with your code.
For more info on the functions that I used please read the docs. Here are the unofficial ones.
I need to be able to change the button position when I click on it. The position changes by a random amount each time I click on it. But all i get is an error. Here's my code:
from tkinter import *
from random import randrange
class Window(Frame):
def position(self):
return randrange(0,400),randrange(0,300)
def __init__(self,master=None):
Frame.__init__(self,master)
self.master = master
self.__init__window()
def __init__window(self):
self.master.title("GUI")
self.pack(fill=BOTH, expand=1)
Button1 = Button(self, text="Click me if you can",command=self.Message)
Button1.place(*position())
menu=Menu(self.master)
self.master.config(menu=menu)
file = Menu(menu)
file.add_command(label="Exit", command=self.client_exit)
menu.add_cascade(label="File",menu=file)
edit = Menu(menu)
edit.add_command(label="Show text", command=self.showText)
menu.add_cascade(label="Edit", menu=edit)
def Message(self):
print("Hello world")
def showText(self):
text = Label(self, text="Hey there!")
text.pack()
def client_exit(self):
exit()
root = Tk()
root.geometry("400x300")
app = Window(root)
root.mainloop()
"Button1.place" is the position of the button that needs to be changed but I am completely clueless how else to do this. I used variables as well.
It seems place() wants keyword arguments. You can let the function position() return a dict and unpack it in the place() statement:
def position(self):
return {'x':randrange(0,400),'y':randrange(0,300)}
Placing the button with:
self.Button1.place(**self.position())
You also need to prefix the button name with "self" to be able to access it from outside the function __init__window().
Then simply add a copy of the place() statement in the button callback function:
def Message(self):
print("Hello world")
self.Button1.place(**self.position())
That works fine for me at least (Python 3.6.5 under win10).
You'll have to reduce the random values generated for x and y or parts of the button will occationally be outside the window...
I'm learning to use tkinter in Python 3.6.4. I am creating a GUI with multiple instances of buttons. Two such instances are:
def createWidgets(self):
# first button
self.QUIT = Button(self)
self.QUIT["text"] = "Quit"
self.QUIT["command"] = self.quit
self.QUIT.pack()
# second button
self.Reset = Button(self)
self.Reset["text"] = "Reset"
self.Reset["command"] = "some other function, tbd"
What I want to learn is how to abstract the instantiation of buttons such that each instance in the createWidgets method is based on a method something like this:
createButton( self, text, command, fg, bg, hgt, wth, cursor ):
What I don't know is how to control the naming of the button as:
self.QUIT
self.Reset
where the property or name following the "." operator can be passed to the createButton as a property by which the button is created and named.
Simply expanding on what Brian said, this code will get you going. The button objects are stored in a widget dictionary. Here is one way to put this together:
import tkinter as tk
import sys
root = tk.Tk()
class CustomButton(tk.Button):
def __init__(self, parent, **kwargs):
tk.Button.__init__(self, parent)
for attribute,value in kwargs.items():
try:
self[attribute] = value
except:
raise
def doReset():
print("doRest not yet implemented")
if __name__ == '__main__':
widget = {}
widget['quit'] = CustomButton(root, text='Quit', command=sys.exit)
widget['reset'] = CustomButton(root, text='Reset', command=doReset)
for button in widget.keys():
widget[button].pack()
root.mainloop()
i have a taskbar-like Frame, which contains custom Buttons with images. But everytime i click on this button, Tkinter displaced the button 1px to the right/buttom.
Is it possible to override this behaviour? Or do i have to derived from Tkinter.Label instead of Tkinter.Button ?
edit:
Adding some code:
import Tkinter
import logging
logger = logging.getLogger(__name__)
class DesktopBtn(Tkinter.Button):
'''
Represents a Button which can switch to other Desktops
'''
_FONTCOLOR="#FFFFFF"
def getRelativePath(self,folder,name):
import os
dir_path = os.path.dirname(os.path.abspath(__file__))
return os.path.abspath(os.path.join(dir_path, '..', folder, name))
def __init__(self, parent,desktopManager,buttonName, **options):
'''
:param buttonName: Name of the button
'''
Tkinter.Button.__init__(self, parent, **options)
logger.info("init desktop button")
self._imagePath=self.getRelativePath('res','button.gif')
self._BtnPresspath = self.getRelativePath('res','buttonP.gif')
self._BtnPressImage = Tkinter.PhotoImage(file=self._BtnPresspath)
self._image = Tkinter.PhotoImage(file=self._imagePath)
self.bind('<ButtonPress-1>',self._on_pressed)
self.bind('<ButtonRelease-1>',self._on_release)
self._parent = parent
self._btnName = buttonName
self._desktopManager = desktopManager
self.config(width=70, height=65,borderwidth=0,compound=Tkinter.CENTER,font=("Arial", 9,"bold"),foreground=self._FONTCOLOR, text=buttonName,wraplength=64,image=self._image, command=self._onClickSwitch)
def _on_pressed(self,event):
self.config(relief="flat")
self.config(image=self._BtnPressImage)
def _on_release(self,event):
self.config(image=self._image)
def _onClickSwitch(self):
self.config(relief="flat")
logger.info("Buttonclickmethod onClickSwitch")
self._desktopManager.switchDesktop(self._btnName)
def getButtonName(self):
return self._btnName
You can disable the animation of a button by returning "break" in the widget's bind, which stops the propagation of bound functions.
So you can either alter the function you normally have bound to the button to return "break".
Or you can add another bind, this does however prevent any binds that are made after this one:
tkButton.bind("<Button-1>", lambda _: "break", add=True)
Not sure whether this works with your specialized button, but how the button moves when it's clicked seems to depend on it's relief style. With relief=SUNKEN, the button seems not to move at all when clicked, and with borderwidth=0 it appears to be indistinguishable from a FLAT button.
Minimal example:
root = Tk()
image = PhotoImage(file="icon.gif")
for _ in range(5):
Button(root, image=image, borderwidth=0, relief=SUNKEN).pack()
root.mainloop()
Note that you set and re-set the relief to FLAT multiple times in your code, so you might have to change them all for this to take effect.
I think I found some kind of a solution using relief and border:
closebut = Button(title, text="X", relief=SUNKEN, bd=0, command=close)
closebut.pack(side=RIGHT)
You can observe that I used relief = SUNKEN and then bd = 0 to get a nice FLAT effect on my button!
Good day.
I am trying to create my own input box for use in my project.
basically what i am trying to do is run my main form which will call the second. the user will provide some data on the second and when the press the ok/close button on the second for the data will be passed back to the first. similar in functionality to the inputbox.
here is what i have created, but being new to python i am not sure where i am going wrong/nor can i quick figure out when to put the return.
My Class is here
import tkinter as tk
class MainWindow():
def __init__(self, parent):
top = self.top = tk.Toplevel(parent)
self.myLabel = tk.Label(top, text='Enter a Grouping Name')
self.myLabel.pack()
self.myEntryBox = tk.Entry(top)
self.myEntryBox.focus_set()
self.myEntryBox.pack()
self.mySubmitButton = tk.Button(top, text='OK', command=self.DestWin)
self.mySubmitButton.pack()
def DestWin(self):
self.top.destroy()
The method to call it is here
abc=configurator.MainWindow(root)
Not exactly sure what you are trying to achieve, but if you are trying to get values from one window to another, below you can find an extended example based on your code.
import tkinter as tk
class MainWindow():
def __init__(self, parent):
top = self.top = tk.Toplevel(parent)
self.myLabel = tk.Label(top, text='Enter a Grouping Name')
self.myLabel.pack()
self.myEntryBox = tk.Entry(top)
self.myEntryBox.focus_set()
self.myEntryBox.pack()
self.mySubmitButton = tk.Button(top, text='OK', command=self.DestWin)
self.mySubmitButton.pack()
def DestWin(self):
# call callback function setting value in MyFrame
self.callback(self.myEntryBox.get())
self.top.destroy()
def set_callback(self, a_func):
self.callback = a_func
class MyFrame(tk.Frame):
def __init__(self, parent, **kwargs):
super().__init__(parent, **kwargs)
self.pack()
self.myLabel1 = tk.Label(parent, text='Click OK to enter the group name')
self.myLabel1.pack()
self.mySubmitButton1 = tk.Button(parent, text='OK', command=self.get_group_name)
self.mySubmitButton1.pack()
def get_group_name(self):
mw = MainWindow(None)
# provide callback to MainWindow so that it can return results to MyFrame
mw.set_callback(self.set_label)
def set_label(self, astr = ''):
self.myLabel1['text'] = astr
root = tk.Tk()
mf = MyFrame(root)
root.mainloop()
The screenshot:
The text from the right window, when OK is pressed, will be shown in the left window. This is achieved through callbacks. MainWindow takes a callback function, and when you press OK, it is executed. The callback is set_label from MyFrame.
Hope this helps.