How can I create MacOS' Help button in tkinter? - python

The Human Interface Guidelines describe a distinctive Help button that isn't the same design as a normal tkinter button. My question is simply, can I create this button in tkinter? I looked in the tkinter docs on effbot, but couldn't find anything.

You can make a button, put a question mark image on it, and set borders to 0.
Here is an example:
from tkinter import *
root = Tk()
root.config(bg="light grey")
helpim = PhotoImage(file="help.gif")
help = Button(root, bd=0, bg="light grey")
help["activebackground"] = "light grey"
help.config(image=helpim)
help.pack()
root.mainloop()
This is the question mark image I used:
You can assign the button a command to show the help as a dialog message box.
Output:

Related

Python Tkinter Gui Hide and show key bind

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()

How to create a button inside of a a Tk canvas in python 3.7

I was wondering how to put a button inside of a canvas using the tkinter module. This question was asked, but it was 5 years ago and for a different version so It was not very convenient for my situation and I am still a beginner so I only understood about 3/4 of the code in the best answer. Here is the question: How to make a Button using the tkinter Canvas widget?
from tkinter import *
root = Tk()
c=Canvas(root,width=200,height=150,bg='blue')
c.pack(side = 'top')
c.create_text(100,50,anchor='c',fill='orange',font='Times 28',text='List')
button = Button(root, text="Quit",command=root.destroy)
button.pack()
mainloop()
When I run this code it creates the button below my Canvas and not on the Canvas. I looked for help on https://docs.python.org/3.7/library/tkinter.html the guide for the IDE I am using. I could not find a way to put the button on the Canvas even though I may or may not have missed something. If this question is seen as not helpful or unnecessary I apologize and will close it immediately.
Version of Python: 3.7
Level: Beginner
Running Code on: IDLE 64-bit
OS: Windows 10
When you use pack() tkinter will place the button on it's master (root), and the area where the canvas is drawn is already occupied.
To place the button on the canvas you should use the function create_window() on the canvas:
from tkinter import *
root = Tk()
c = Canvas(root,width=200,height=150,bg='blue')
c.pack(side = 'top')
c.create_text(100, 50, anchor='c', fill='orange', font='Times 28', text='List')
button = Button(root, text="Quit", command=root.destroy)
canvas_widget = c.create_window(100, 100, window=button)
root.mainloop()
delete button.pack()
try use the code below
button = Button(root, text="Quit", command=root.destroy)
c.create_window(10, 10, anchor=NW, window=button)

How do I disable keyboard input into an Entry widget, disable resizing of tkinter window and hide the console window?

I am making a calculator using tkinter and I wish to do the following:
Disable keyboard input for the Entry widget so that the user can only input through the buttons.
Even after disabling keyboard input for the Entry widget, I wish to be able to change the background and foreground of the widget.
I wish to hide the console window because it is totally useless in the use of this calculator.
I don't want to let the user resize the root window. How do I disallow the resizing of the root window?
Here is my code so far...
from tkinter import *
root = Tk()
root.title("Calculator")
root.config(background="black")
operator = ""
textVar = StringVar()
def valInput(number):
global operator
operator+=str(number)
textVar.set(operator)
display = Entry(root, textvariable=textVar, font=("Arial", 14, "bold"), bg="lightblue", fg="black", justify="right")
display.grid(row=0, column=0, columnspan=4)
btn7 = Button(root, font=("Arial", 12, "bold"), bg="orange", fg="red", text="7", command= lambda : valInput(7))
btn7.grid(row=1, column=0)
"""
And more buttons...
"""
root.mainloop()
As you can see, I can input into the Entry widget using buttons but later on, after the calculator is complete, if the user inputs characters like abcd... it will cause problems and show errors. How do I disallow keyboard entry so that I can avoid these errors?
I want to make my calculator a bit colorful. I changed the color of the root window, the buttons and also the color of the Entry widget. Is there any way to change the color of the widget even after it is disabled?
I don't need the console window while using this calculator. How do I hide it?
If I resize the root window, the calculator becomes ugly, besides, resizing the window isn't necessary. So how do I prevent the user from resizing the window?
To be able to disable keyboard input in Entry(args)
Set the state to disabled:
display = Entry(root, state=DISABLED)
To be able to disable the feature of resizing the tkinter window (so that you can't drag and stretch it.
root.resizable(0,0)
To be able to make the command prompt window disappear. (I just want the tkinter window.
Rename the file with a .pyw extension (assuming you are using windows)
Don't use from tkinter import * it's really not recommended because it pollutes the main namespace with every public name in the module. At best this makes code less explicit, at worst, it can (and it will) cause name collisions.
Have the right reflexes, use import tkinter or import tkinter as tk instead
this should work, you have to use the disabledbackground option :
import tkinter as tk
root = tk.Tk()
display = tk.Entry(root,font=('Arial', 20, 'bold'), disabledbackground='lightblue', state='disabled')
display.pack()
root.resizable(0,0)
root.mainloop()

Two text boxes and many buttons python

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.

Keep a menu open in Tkinter

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()

Categories