tkinter with mac osx - python

I have a very basic question related to Python 3. I have started to learn Python but some things are confusing me.
First off, since I want to create a python script that is a GUI, I have imported the tkinter module. The code works in IDLE, but never works when I run it from the terminal. Whenever I run the script from the terminal, I see this traceback error:
Traceback (most recent call last):
File "test1.py", line 9, in <module>
mGui.geometry("geometry 480x480")
File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/tkinter/
__init__.py", line 1607, in wm_geometry
return self.tk.call('wm', 'geometry', self._w, newGeometry)
_tkinter.TclError: bad geometry specifier "geometry 480x480"
Basically, what I am trying to do is create a Python GUI script, save it, and execute it via my terminal whenever I need it.
Here is the code:
#!/usr/bin/env python3
import sys
from tkinter import *
mGui =Tk("")
mGui.geometry("geometry 480x480")
mGui.title("Leilani spelling test")

You don't add the word "geometry" to the argument of the geometry method. Try this instead:
#!/usr/bin/env python3
import sys
from tkinter import *
mGui =Tk("")
mGui.geometry("480x480")
mGui.title("Leilani spelling test")
# You'll want to add this to enter the event loop that causes the window to be shown
mGui.mainloop()
Here are some other GUI configurations that you may need in the future (I personally had trouble finding/applying all the information):
mGui.overrideredirect(1) # Remove shadow & drag bar. Note: Must be used before wm calls otherwise these will be removed.
mGui.call("wm", "attributes", ".", "-topmost", "true") # Always keep window on top of others
mGui.geometry("100x100+500+500") # Set offset from top-left corner of screen as well as size
mGui.call("wm", "attributes", ".", "-transparent", "true") # Remove shadow from window
mGui.call("wm", "attributes", ".", "-fullscreen", "true") # Fullscreen mode
mGui.call("wm", "attributes", ".", "-alpha", "0.9") # Window Opacity 0.0-1.0

Related

Run .py file in system tray rather than in taskbar window

I have a .py file that runs via Task Scheduler when I log into my computer, and it's constantly listening for specific keyboard events to happen. Rather than having it run in the Python Console window on the Taskbar, I'd like to get it to run in the system tray with a Python icon to make it more clean.
I have discovered the pysystray library, and can successfully make a Python icon in the system tray with a right-click menu which will allow me to quit the icon.
from os import path as path
import PIL.Image
import pystray
def on_clicked(icon, item):
if str(item) == "Quit":
icon.stop()
dir_path = path.dirname(path.realpath(__file__))
icon_image = "\\Icon\\Python.ico"
full_path = dir_path + icon_image
image = PIL.Image.open(full_path)
icon = pystray.Icon(
"Testing",
image,
menu=pystray.Menu(pystray.MenuItem("Quit", on_clicked)),
)
icon.run()
However, I can't figure out how to get my existing Python file to run under the icon. It seems like there are two separate events happening; one for launching the icon, and another for running the rest of the code. I would want to make sure that if the "Quit" option is chosen, then the .py file would also stop.
Also, it would be really helpful to have a separate right-click menu item which will display either Python cmd line text since it's constantly being overwritten in my main .py file with a print statement.
Any help/insight would be really appreciated!
The pystray.run function accepts an optional callback function as a parameter. The callback function is run in a separate thread and should be closed normally by ln 9: icon.stop().

Why gdspy library give a tkinter tcl error when trying to use the layout viewer

I'm using the gdspy library which uses tkinter to show a layout preview.
But when trying to use:
gdspy.LayoutViewer(library=None, cells=[cell])
python give me a:
Traceback (most recent call last):
File ".\InductorGen.py", line 726, in <module>
gdspy.LayoutViewer(library=None, cells=[cell])
File "C:\Users\Maël\Anaconda3\lib\site-packages\gdspy\viewer.py", line 182, in __init__
self.grid(sticky='nsew')
File "C:\Users\Maël\Anaconda3\lib\tkinter\__init__.py", line 2226, in grid_configure
+ self._options(cnf, kw))
_tkinter.TclError: cannot use geometry manager grid inside . which already has slaves managed by pack
here is the code from the layout viewer of gdspy: gdspy_layoutviewer
Looking through the gdspy code, it appears that it assumes that the caller is using grid inside of the root widget. More correctly, it's assuming there are no other widgets in the root window.
It uses grid to inject itself into row zero and column zero of the root window. If your code (or Anaconda itself) is using pack in the root widget, that would explain the error that you get.

Tkintter error: cannot use geometry manager grid inside . which already has slaves managed by pack

from tkinter import *
import random
import time
tk=Tk()
canvas=Canvas(tk,width=1000,height=1000)
canvas.pack()
i=0
x1=-50
y1=0
choice=None
canvas.create_text(500, 300, text="Choose one.", font='TNR 20 bold')
def car():
choice='car'
return choice
button1=Button(tk,text='car',width=10,command=car)
button1.grid(row=1,column=5)#I do not see anything wrong here
tk.mainloop()
This is just part of my code if you are wondering why I have so many unused variables.
This is what is says when I run the code:
Traceback (most recent call last):
File "(You dont need to know the file name)", line 180, in
button1.grid(row=1,column=5)
File "C:\Users\()\AppData\Local\Programs\Python\Python37-32\lib\tkinter\__init__.py", line 2223, in grid_configure
+ self._options(cnf, kw))
_tkinter.TclError: cannot use geometry manager grid inside . which already has slaves managed by pack
You cannot mix pack() and grid()
from the docs:
Warning: Never mix grid and pack in the same master window. Tkinter will happily spend the rest of your lifetime trying to negotiate a solution that both managers are happy with. Instead of waiting, kill the application, and take another look at your code. A common mistake is to use the wrong parent for some of the widgets.
you are calling canvas.pack() but to the same tk object you add a button on which you call the grid() function.

Bash script to run Python Tkinter GUI

I am trying to write a Bash script so that I can run my program on a double click. The program uses tkinter and the GUI is the only thing I need to see. My bat file is the following:
python BudgetGUI.py &
This runs the code and successfully prints any print statements I have throughout my code but it never opens up the GUI. It simply runs through and closes immediately.
How can I modify the bash script to run the GUI?
Thanks in advance!
Edit Solutions for both mac and pc would be great, though at the moment I am on PC. I am working in Python3.
You need to add a call to mainloop(). I can't say for sure without seeing your code, but probably you need to add root.mainloop() to the bottom.
You don't need a bash or bat file. For your mac, just add a shebang and make the file executable. For Windows, add a shebang and associate the file with py.exe.
If you want to suppress the command line from popping up along with the GUI, rename your file with a .pyw extension.
Make the window made with tkinter stay out of IDLE*
if you have
root = Tk()
at the end put
root.mainloop()
if you have another name for that like:
window1 = Tk()
then, at the end put
window1.mainloop()
and so for any name you gave to the istance of Tk()
A little example
import tkinter as tk
class Window:
root = tk.Tk()
label = tk.Label(root, text = "Ciao").pack()
app = Window()
app.root.mainloop()
Python 2
The code above is for python 3.
For python 2 you just need to change the first line (Tkinter with T, not t)
import Tkinter as tk

Accessing selection clipboard from ipython

I want to access the text in the clipboard from within ipython.
I got this far (not even sure if this is the best way, just found by poking around in ipython magics sources):
import IPython
from IPython.core.hooks import clipboard_get
ip = IPython.get_ipython()
my_string = clipboard_get(ip)
And it kinda works for stuff I've copied manually, but I want to get the "other" clipboard - the one you get when you use the middle mouse click. The selection buffer or whatever it's called.
Any ideas?
You can get X Window's "middle mouse button" selection (called the PRIMARY selection) through Tkinter:
import Tkinter # Replace "Tkinter" with "tkinter" for Python 3.x.
tk = Tkinter.Tk()
tk.withdraw()
print(tk.selection_get())
Another solution is to run xclip and get its output. (If you don't have xclip installed it can be found in most Linux distributions' package repositories.)
import subprocess
print(subprocess.check_output(['xclip', '-o', '-selection', 'PRIMARY']))

Categories