Tkinter Linux version of "<Control-Shift-u>" - python

What's the Linux version of "<Control-Shift-u>" for keybindings in Tkinter? You might be tempted to think it's exactly that, but, alas, it does not seem to be. For instance, the Linux version of "<Control-Shift-Tab>" is "<Control-ISO_Left_Tab>". I've searched and haven't found any documentation for this.

The following will do what you want:
from Tkinter import *
def proof(event=None):
print 'ping'
root = Tk()
frame = Frame(root, height=100, width=100)
frame.focus_set()
frame.bind('<Control-Shift-KeyPress-U>', proof)
frame.pack()
root.mainloop()
The u becomes capitalized because of the shift modifier and you want to capture the KeyPress event.

Related

Tkinter Dark Theme by Default with Python 3.10?

I've just updated Python to 3.10 and when I run Tkinter programs they appear with a dark theme which I've never seen before. I'd like to go back to the standard light theme but I can't figure out how to. There doesn't seem to be any obvious documentation online regarding this.
Here is some quick sample code that just displays a grid to reproduce the dark theme (I'm using Python 3.10 and Visual Studio Code 1.61.2):
from tkinter import *
root = Tk()
class Something:
def __init__(self, parent, col, row):
canvas = Canvas(parent, bd=1, relief=SOLID, highlightthickness=0, width=30, height=30)
canvas.grid(column=col, row=row)
frame = Frame(root, bd=1, relief=SOLID)
frame.grid(padx=50, pady=50)
for i in range(11):
for j in range(11):
Something(frame, i, j)
root.mainloop()
There might be a problem with your default system UI.
I don't have a MAC so I can't test this, but try changing your default system ui to light, if MAC even has that option...

tkinter fill=Y does not work with Button [duplicate]

I am familiarizing myself with Tkinter, and I am attempting to write a very simple program, which displays a button in a window, using the pack geometry manager.
I was experimenting with various configuration options for pack(), such as expand, fill, and side, and I've run into a peculiar problem. I have written the following code:
from Tkinter import *
root = Tk()
widget = Button(root, text='text')
widget.pack(expand=YES, fill=BOTH)
root.mainloop()
The problem is that the button expands to fill the window in the horizontal direction, but not the vertical direction. This is the same result that I get if instead of specifying fill=BOTH I use fill=X. In addition, if I specify instead fill=Y the button does not expand in either direction. Something seems to be going wrong with the fill in the vertical direction, and I cannot figure out what it might be.
I attempted to Google this problem and surprisingly found no mention of this happening to anyone else. I am using a Mac with OS X Yosemite and running python 2.7.5. I also attempted to compile with python 3.4.1 and saw no change.
Edit:
Based off of the answer and comments below, it is clear that there is nothing wrong with my code, because it seems to work on other machines. If not an error in the code, does anyone know what could possibly be causing the button to not stretch vertically when I run the above code?
This is a feature of native buttons on OSX. Buttons on OSX will be a fixed height and will not expand vertically. There is nothing you can do, short of using a different widget such as a label.
try running this code to see the behavior of fill and expand
from Tkinter import *
root = Tk()
root.geometry("500x500")
widget = Button(root, text='text1')
widget.pack(fill=X, expand=1)
widget = Button(root, text='text2')
widget.pack(fill=Y, expand=1)
widget = Button(root, text='text3')
widget.pack(fill=BOTH, expand=1)
root.mainloop()
Argument fill does fill in vertical direction as well
I am also beginner, defining geometry for fill was missing in your code as given below:
from Tkinter import *
root = Tk()
root.geometry("600x400")
widget = Button(root, text='text')
widget.pack(expand=YES, fill=BOTH)
root.mainloop()

How do I display a tkinter application in fullscreen on macOS?

I am just learning python, and I am trying to make a window go full screen, which I have achieved, but I am now wanting to get rid of the title bar across the top. It currently looks like the image below, but I want it to also go over the Mac top toolbar at top (like a splash screen).
from tkinter import *
root = Tk()
root.attributes('-fullscreen', True)
root.attributes('-topmost', True)
root.overrideredirect(True)
def quitApp():
# mlabel = Label (root, text = 'Close').pack()
root.destroy()
# placing the button on my window
button = Button(text = 'QUIT', command = quitApp).pack()
I believe what you want to do is use
root.wm_attributes('-fullscreen','true')
Try this instead. It should do the trick.
from tkinter import *
root = Tk()
root.wm_attributes('-fullscreen','true')
def quitApp():
root.destroy()
button = Button(text = 'QUIT', command = quitApp).pack()
root.mainloop()
If this does not work because of the MacOS then take a look at this link This useful page has sever examples of how to manage mack windows in tkinter. And I believe what you may need to get borderless fullscreen.
This bit of code might be what you need:
root.tk.call("::tk::unsupported::MacWindowStyle", "style", root._w, "plain", "none")
Note: If you do use this option then you will need to remove root.wm_attributes('-fullscreen','true') from your code or just comment it out.
Update:
There is also another bit of code for tkinter 8.5+.
If you are using python with tkinter 8.5 or newer:
root.wm_attributes('-fullscreen', 1)

tkinter mac, minimize screen

I'm using tkinter in python for mac.
Simply put, I want to override the "minimize" button's functionality.
I'm already overriding the "X" button functionality in this fashion:
root = Tk()
root.protocol('WM_DELETE_WINDOW', doClose)
What I tried:
I looked into the WM states and there is no WM_ICONIFY_WINDOW or WM_MINIMIZE_WINDOW. I also tried working with WM_SAVE_YOURSELF but couldn't catch any interrupt.
what's the best known way of making tkinter do what I want when people hit "minimze" rather than the default settings?
Thanks!
There is no standard WM_PROTOCOL message for minimization. It seems, the best solution - catching <Unmap> events:
from Tkinter import *
root = Tk()
def callback(event):
print event
frame = Frame(root, width=100, height=100)
frame.bind("<Unmap>", callback)
frame.pack()
root.mainloop()
Related links: 1, 2, 3

Gedit problems with python

I'm just learning Python in school and we were suppose to draw something (code it in gedit for python) on canvas (Tkinter). Instead of getting something drawn up I only get an empty canvas. It looks like this on my computer. The code is correct as I copied it from another web page.
from Tkinter import *
master=Tk()
w=Canvas(master, width=200, height=100)
w.pack
w.create_line(0,0,200,100)
w.create_line(0,100,200,0, fill="red", dash=(4,4))
w.create_rectangle(50,25,150,75, fill="blue")
mainloop()
Actually, the code is not correct. The person who wrote it forgot to actually call the pack method. You need to add () after it to do this:
from Tkinter import *
master=Tk()
w=Canvas(master, width=200, height=100)
########
w.pack()
########
w.create_line(0,0,200,100)
w.create_line(0,100,200,0, fill="red", dash=(4,4))
w.create_rectangle(50,25,150,75, fill="blue")
mainloop()
Otherwise, the canvas will never be placed on the window.
P.S. You should note that not everything you find on the Web is guaranteed to be correct. :)

Categories