How to bind the pop up menu to a label in Tkinter - python

I want to limit the area where the popup menu can be triggered
My current code allows the popup menu be triggered anywhere in the tkinter window when the user right clicks
from tkinter import *
root = Tk()
w = Label(root, text="Right-click to display menu", width=40, height=20)
w.pack()
popup = Menu(root, tearoff=0)
popup.add_command(label="Next") # , command=next) etc...
popup.add_command(label="Previous")
popup.add_separator()
popup.add_command(label="Home")
def do_popup(event):
try:
popup.tk_popup(event.x_root, event.y_root, 0)
finally:
popup.grab_release()
w.bind("<Button-3>", do_popup)
b = Button(root, text="Quit", command=root.destroy)
b.pack()
mainloop()
I want the popup menu to be triggered when the user right clicks over the label "Right-click to display menu" only

Your code is working exactly as designed. You've created a really huge label widget (40 characters wide, 20 characters high, or roughly 350x325 pixels depending on your system font and resolution settings). So while you think you're clicking outside the label, you're not since it takes up the whole window.
To see what I mean, give your label a distinctive background color. For example:
w = Label(root, text="Right-click to display menu", width=40, height=20, background="pink")
The above results in a window that looks like the following image. Anywhere you click that is pink is part of the label, and thus will show the menu.

Related

Buttons not being placed inside the correct frame in tkinter

I am a newbie trying to use tkinter to build a GUI for an application. So far, I have a frame that I'd like to put several buttons into. However, every time I attempt to position this button, it isn't placed properly, being put outside of the frame itself. I wouldn't like to use the place function because of the several buttons I have to dynamically generate coming from an excel sheet so I was hoping to use the grid function instead.
Here is what I have so far
from tkinter import *
from customtkinter import *
window = Tk()
window.geometry("1920x1080")
window.state("zoomed")
window.title("My Company's Description Printer")
main_frame = CTkFrame(window, width=1920, height=1080, fg_color="grey21")
main_frame.place(x=0, y=0)
title = Label(main_frame,
text="My Company",
bg="grey21",
fg="white",
font=("Trajan Pro", 20)).place(x=626, y=30)
button_frame = CTkCanvas(main_frame,
width=800,
height=600,
highlightthickness=3,
highlightbackground="black",
relief="ridge",
bg="grey19").place(x=60, y=110)
test_button = CTkButton(button_frame, text="test").grid(row=0, column=0)
window.mainloop()
Example of code being ran
As you can see, the button is being placed in the top left corner of the entire window rather than the top left corner of the black bordered button frame. Any help would be appreciated. Thank you so much.
Note that button_frame is None because it is the result of .place(...), so the button (test_button is None as well due to same reason) is a child of the root window instead of the instance of CTkCanvas. .place(...) should be called in separate line.
Also .create_window() is used instead of tkinter layout manager to put widget into a canvas:
...
button_frame = CTkCanvas(main_frame,
width=800,
height=600,
highlightthickness=3,
highlightbackground="black",
relief="ridge",
bg="grey19")
# call .place(...) in separate line
button_frame.place(x=60, y=110)
test_button = CTkButton(button_frame, text="test") # don't use .grid(row=0, column=0)
# use .create_window() to put widget into canvas
button_frame.create_window(0, 0, window=test_button, anchor="nw")

I wanna add a small buttons bar down the note and when i do buttons dont appear why?

am trying to make a notepad app and I wanted to add a small bar at the end with buttons to do custom stuff but when I try to add a button it doesn't appear on the program what did I do wrong?
here is the script
from gc import callbacks
import re
from tkinter import *
from tkinter import ttk
import time
from turtle import right
root = Tk()
root.title('Notepad')
root.iconbitmap('C:/notes.ico')
root.geometry("500x500")
root.tk.call("source", "C:/Users/Hero/Documents/Visual Studio code/My project/azure.tcl")
root.tk.call("set_theme", "dark")
def change_theme():
if root.tk.call("ttk::style", "theme", "use") == "azure-dark":
root.tk.call("set_theme", "light")
else:
root.tk.call("set_theme", "dark")
style=ttk.Style()
style.theme_use('azure-dark')
style.configure("Vertical.TScrollbar", background="grey", bordercolor="black", arrowcolor="white")
scroll = ttk.Scrollbar(root, orient='vertical')
scroll.pack(side=RIGHT, fill='y')
text=Text(root, font=("Georgia, 24"), yscrollcommand=scroll.set, bg='#292929')
scroll.config(command=text.yview)
text.pack()
#button am talking about
fonts = ttk.Button(root, text='Size and Font', style='Accent.TButton')
fonts.pack()
root.mainloop()
Update: It worked when i had the buttons at the top but at the bottom it doesn't show up
The text box is too tall for a window with size 500x500, so the button below the text box is out of the viewable area of the window.
You can set the width and height options of the text box to a smaller values and use text.pack(fill='both', expand=1)` to expand the text box to fill the window:
# set width and height to smaller values
text = Text(root, font=("Georgia, 24"), yscrollcommand=scroll.set, bg='#292929', width=1, height=1)
# then expand it to fill the window
text.pack(fill='both', expand=1)
i fixed by adding a frame then making it at the bottom then putting the button at this frame thanks everyone that tried to help

Messed Up Spacing After Button Click in Tkinter

I'm trying to create a GUI using Tkinter for a Pip-boy from Fallout 3. I'm running into a problem where after I click a button in one of the frames, the spacing between the buttons gets messed up. This spacing change happens for all but one of the buttons in the frame (the Lockpick one).
This is what I want the button spacing to look like (what it looks like before I click a button):
This is what happens after I click a button (in this case the Barter one)
Here is the code I am using:
from tkinter import *
# to read descriptions of each skill from a text file
with open("skills.txt") as f:
lines = f.readlines()
# function that updates the label with a different description when the corresponding button is clicked
def show_skill_desc(index):
desc['text'] = lines[index-1]
# makes the window and frame
root = Tk()
root.geometry("1024x600")
root.title("Skills")
frame = Frame(root)
frame.grid()
# creates the label
Label(root, text="Stats").grid(row=0, column=0)
# list of skills which will each have their separate labels
skills_list = ["Barter", "Big Guns", "Energy Weapons", "Explosives", "Lockpick", "Medicine",
"Melee Weapons", "Repair", "Science", "Small Guns", "Sneak", "Speech", "Unarmed"]
# placing the label in the frame
desc = Label(root, text="", padx=30, wraplength=600, justify=LEFT)
desc.grid(row=2, column=1)
# creates a button for each skill
button_list = []
for i in range(12):
button_list.append(Button(root, text=skills_list[i], width=40,
height=2, command=lambda c=i: show_skill_desc(button_list[c].grid_info()['row']), padx=0, pady=0))
button_list[i].grid(row=i+1, column=0)
root.mainloop()
The purpose of the GUI is to display the description of each skill, when the button for a skill is clicked.
Does anyone know why the spacing change happens? Thank you!

How to set up personal picture for every state of button in tkinter?

I am creating program and want to use buttons with personal design, but as I find out tkinter can just allow you to set up one image for all states of button, is this possible to make it like in HTML and CSS?
P.S.: In result I also want to have button without deep effect when click on it
This is a main menu for my program which calculate student rating, button is used to choose category
import tkinter as tk
root = tk.Tk()
w = 854
h = 480
root.title("Calculator 2.0")
root.geometry('%dx%d' % (w, h))
root.resizable(False, False)
filename=tk.PhotoImage(file='.\\Main_Menu.png')
background_label = tk.Label(root, image=filename)
background_label.place(x=0, y=0, relwidth=1, relheight=1)
vp = tk.PhotoImage(file='.\\Buttons\\But_VP_idle.png')
but_vp = tk.Button(root, image=vp, bg='#131313', bd=0,
activebackground='#BC51CD')
but_vp.place(relx=0.5, rely=0.5, anchor=tk.CENTER)
root.mainloop()
I expect for nice looking buttons, but the actual output is not what i expected.

controlling the position of the scrollbar in Tkinter

Am trying to Learn Tkinter. The following program works fine. It creates a textbox and a scrolllbar connected to the textbox. There is also a button, which when pressed inserts a line into the text box. Now what I want to do is to move the scrollbar such that when the text goes beyond the first screen, the scrollbar automatically scrolls down so that the user sees only the last line of the input. What I want to do is for the text box to behave like an extended stats window, so that the user is able to see the progress so far. Right now the user will continuously have to scroll down by using the scrollbar to the last line. I want this process to be automatic. Is there a command which I can use to do that?
Thanks!
from Tkinter import *
root = Tk()
def checkFn():
text.insert(END, ' --> The check button has been pressed ... \n')
# ............... Frame 1 .................................
frame1 = Frame(root)
frame1.pack(side=TOP, fill=X)
b = Button(frame1, text='Check', command=checkFn)
b.pack(side=LEFT, padx=5)
# .............. Text Frame .................................
frame10 = Frame(root)
frame10.pack(side=TOP, fill=X)
scrlBar = Scrollbar(frame10)
scrlBar.pack(side=RIGHT, fill=Y)
text = Text(frame10, yscrollcommand=scrlBar.set)
text.insert(INSERT, 'abcd\n')
text.pack(side=LEFT, fill=BOTH)
scrlBar.config(command=text.yview)
root.mainloop()
The easiest method I would say is see(index)
In your checkFn() method, use text.see(END)

Categories