Found a rather interesting way to create menus in the Tkinter GUI - Menubutton. But unfortunately this code does not work (or rather, when you click on Menubutton the bound Menu does not open):
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
root.option_add("*Menu.borderWidth", "0")
root.option_add("*Menu.activeBorderWidth", "0")
root.option_add("*Menu.background", "black")
style = ttk.Style(root)
menu = tk.Menu(root)
btn_menu = ttk.Menubutton(root, text='fegvd')
btn_menu.pack()
file = tk.Menu(btn_menu, tearoff=0, foreground='white')
file.add_command(label='ГЫГ')
style.configure('TMenubutton', background='black', foreground='white', indicatoron=0, menu=file, direction='delow', state='active')
root.mainloop()
Although, if I use not ttk.Menubutton, but tk.Menubutton, then everything works:
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
root.option_add("*Menu.borderWidth", "0")
root.option_add("*Menu.activeBorderWidth", "0")
root.option_add("*Menu.background", "black")
menu = tk.Menu(root)
btn_menu = tk.Menubutton(root, text='fegvd')
btn_menu.pack()
file = tk.Menu(btn_menu, tearoff=0, foreground='white')
file.add_command(label='ГЫГ')
btn_menu.configure(background='black', foreground='white', indicator=0, menu=file, state='active')
root.mainloop()
Why? Tell me, please, what is the problem?
You cannot use the style to associate the menu with the menubutton. You need to do it exactly like you do with the tk menu:
btn_menu.configure(menu=file)
Related
I have developped a app in Python (tested with 3.8 and 3.9 on Windows 10) with Tkinter. I am using a Combobox and a Treeview. I want to change dynamically the width of dropdown listbox and I could do it by changing the style of TCombobox with the parameter postoffset.
However, everytime I click on the Combobox, the table next to it moves and extends its width by itself. I really don't know where this problem comes from.
I have created a simple code so that you can reproduce the problem.
import tkinter as tk
from tkinter import ttk
import tkinter.font as tkfont
def combo_configure(event, style):
combo = event.widget
long = max(combo.cget('values'), key=len)
font = tkfont.Font(family="Helvetica", size=10)
width = max(0,font.measure(long.strip() + '0') - combo.winfo_width())
style.configure('TCombobox', postoffset=(0,0,width,0))
win = tk.Tk()
win.geometry("850x250")
style = ttk.Style()
f = tk.Frame(win)
f.configure(bg="black")
f.pack()
combo = ttk.Combobox(f, style="TCombobox", values=["It's a test on Combobox style."], width=10)
combo.bind('<ButtonPress>', lambda e : combo_configure(e, style))
combo.pack(side="left")
tree = ttk.Treeview(f, column=["Column 1"], show='headings', height=10)
tree.heading(0, text="Column 1")
tree.column(0, anchor=tk.CENTER, width=125)
tree.pack()
win.mainloop()
Thanks.
Is it possible to make a Tkinter yes/no messagebox with a checkbox, for something like 'Never ask me again'?
Or would I have to create another window, create my own Labels and CheckButtons, and basically create my own dialog?
You should create your own dialog box then.
Here’s what you can do:
from tkinter import *
def popup():
popupWin = Toplevel()
popupWin.title(“Continue?”)
checkVariable = IntVar()
lbl = Label(popupWin, text=“Continue?)
lbl.pack()
btn2 = Button(popupWin, text=“Yes”)
btn2.pack()
btn3 = Button(popupWin, text=“No”)
btn3.pack()
checkBox = Checkbutton(popupWin, text=“Don’t ask again”, variable=checkVariable)
root = Tk()
btn = Button(root, text=Message Box, command=popup)
root.mainloop()
So till now I want to make a simple button but it gives me an error screen, what am I doing wrong? Here's my code:
import tkinter as tk
import math
import time
tk = tk.Tk()
tk.geometry()
tk.attributes("-fullscreen", True)
exit_button = tk.Button(tk, text = "Exit", height = 2, width = 2, command = tk.destroy)
exit_button.place(x=1506, y=0)
tk.mainloop()
You are shadowing tk with something else:
import tkinter as tk
root = tk.Tk()
root.geometry()
root.attributes("-fullscreen", True)
exit_button = tk.Button(root, text="Exit", height=2, width=2, command=root.destroy)
exit_button.place(x=1506, y=0)
tk.mainloop()
You cannot use tk = tk.Tk(), because you are also referring to tkinter as tk. So either:
Change your imports(not recommended):
import tkinter as _tk
tk = _tk.Tk() # And so on..
or change your variable name(recommended):
root = tk.Tk() # And change tk.geometry to root.geometry() and so on
hy I am working on tkinter python project.
I am trying to position text to the Left inside OptionMenu using anchor option but it does not seem to work. I am using ttk theme widgets.
Here is the code that I am trying currently.
s = ttk.Style()
s.configure('my.TMenubutton', font=("Cambria", fontSize, "bold"), background="white", anchor = W )
shapeMenu = ttk.OptionMenu(shapeFrame, shape, myShapes[1], *myShapes, style='my.TMenubutton', command=getShapes)
What I am doing wrong ?
It seems like there is no anchor option under TMenubutton. I tried TLabel, and it worked. Don't know if there is any side effect though.
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
root.geometry('300x100')
CHART_TYPES = ('Reticle', 'Circle Grid', 'Checkerboard', 'Grille', 'Slanted Edge MTF')
s = ttk.Style()
s.configure('my.TLabel', font=("Cambria", 10, "bold"), background="white", anchor = 'w' )
chart_type = tk.StringVar()
chart_type.set(CHART_TYPES[0])
# chart_selector = tk.OptionMenu(root, chart_type, *CHART_TYPES)
chart_selector = ttk.OptionMenu(root, chart_type, *CHART_TYPES, style='my.TLabel')
# chart_selector.configure(anchor='w')
chart_selector.pack(expand=True, fill='x')
root.mainloop()
I've found that when a toplevel widget calls a messagebox dialog (like "showinfo"), the root window is showed up, over the toplevel. Is there a way to set the Toplevel window as the master of the messagebox dialog ?
Here is a script to reproduce this :
# -*- coding:utf-8 -*-
# PYTHON 3 ONLY
from tkinter import *
from tkinter import messagebox
root = Tk()
root.title('ROOT WINDOW')
Label(root, text = 'Place the toplevel window over the root window\nThen, push the button and you will see that the root window is again over the toplevel').grid()
topWindow = Toplevel(root)
topWindow.title('TOPLEVEL WINDOW')
Label(topWindow, text = 'This button will open a messagebox but will\ndo a "focus_force()" thing on the root window').grid()
Button(topWindow, text = '[Push me !]', command = lambda: messagebox.showinfo('foo', 'bar!')).grid()
# --
root.mainloop()
You can set the parent argument to topWindow for the showInfo command:
Button(..., command=lambda: messagebox.showInfo(parent=topWindow, ...))
See also:
http://effbot.org/tkinterbook/tkinter-standard-dialogs.htm
This may address more current versions.
#TEST AREA forcommands/methods/options/attributes
#standard set up header code 2
from tkinter import *
from tkinter import messagebox
root = Tk()
root.attributes('-fullscreen', True)
root.configure(background='white')
scrW = root.winfo_screenwidth()
scrH = root.winfo_screenheight()
workwindow = str(1024) + "x" + str(768)+ "+" +str(int((scrW-1024)/2)) + "+" +str(int((scrH-768)/2))
top1 = Toplevel(root, bg="light blue")
top1.geometry(workwindow)
top1.title("Top 1 - Workwindow")
top1.attributes("-topmost", 1) # make sure top1 is on top to start
root.update() # but don't leave it locked in place
top1.attributes("-topmost", 0) # in case you use lower or lift
#exit button - note: uses grid
b3=Button(root, text="Egress", command=root.destroy)
b3.grid(row=0,column=0,ipadx=10, ipady=10, pady=5, padx=5, sticky = W+N)
#____________________________
root.withdraw()
mb1=messagebox.askquestion(top1, "Pay attention: \nThis is the message?")
messagebox.showinfo("Say Hello", "Hello World")
root.deiconify()
top1.lift(aboveThis=None)
#____________________________
root.mainloop()
Button(..., command=lambda: messagebox.showinfo("The Title", "A piece of text", parent=topWindow))
This will definitely work. The syntax of the messagebox is
messagebox.Function_Name(title, message [, options])
setting the parent argument is one of the options
add topWindow.attributes("-topmost", 1)
after defining the Toplevel, and this should fix the problem