EDIT: This seems to be a problem restricted to Tcl/Tk on Mac OS systems. So if you have no experience with that, this topic might be moot...
I want to have a Python script that does two things:
Ask the user for a file name via a Tkinter file dialog.
Plot some data from said file.
The problem is, matplotlib uses Tkinter for the graphical representations, and whenever I call pyplot.show() in non-interactive mode, the (before closed) file dialog pops up again. It seems to me like pyplot.show() gathers a list of all Tkinter windows and shows them all. I did not find any help on this however. I tried for both Python 2.7 and 3.3, since a lot of the Tkinter module seems to have changed, but it's the same phenomenon. The slightly strange workaround I came up with is to go into matplotlib interactive mode and then keep the windows open with a raw_input() command.
Here is a minimal code snippet that works in Python 2 and 3 to show the problem:
import matplotlib.pyplot as plt
# import Tkinter GUI (changes from Python 2.x to 3.x)
try:
import Tkinter
except (ImportError):
import tkinter as Tkinter
try:
from tkFileDialog import askopenfilename
except (ImportError):
from tkinter.filedialog import askopenfilename
root = Tkinter.Tk()
root.withdraw()
input_filename = askopenfilename(master=root)
# This seemed promising, but it doesn't help
root.destroy()
plt.figure()
# uncommenting this to switch to interactive mode is a workaround
#plt.ion()
plt.show()
# Python 2.x and 3.x compatible wait for input:
try: input = raw_input
except NameError: pass
# Wait for keystroke (for interactive mode)
input("Press enter when done...")
I'm sorry if I'm missing something obvious here, I am not that well-versed in Python, and I did not find satisfying information on this problem. But my gutt tells me there has to be a simple and elegant solution for this.
System information (most recent versions I tried):
Python 3.3 (from MacPorts)
matplotlib 1.3.x (built from github master)
Mac OS X 10.8.3
Tcl/Tk 8.6.0 (from MacPorts)
Thanks,
Floh
Related
I'm learning python via this course
I'm using Ubuntu 22.04 and Python 3.10.7
I have to use pyautogui like this
import pyautogui
from time import sleep
sleep(1)
pyautogui.write( ' This is written by a computer')
On the video, the tutor switches to the text editor and just starts writing. Nothing happens on mine when I run this code.
So I go to the pyautogui docs and try the alertbox code sample just to see if I can get anything working.
import pyautogui
pyautogui.alert(text='', title='', button='OK')
This gives me the error AssertionError: Tkinter is required for pymsgbox
Similarly, on the video, using matplotlib allows drawing a graph with this code
import matplotlib, matplotlib.pyplot as plt
from time import sleep
plt.plot([1, 2, 3, 4])
plt.ylabel('some numbers')
plt.show()
It tells me Matplotlib is currently using agg, which is a non-GUI backend, so cannot show the figure. Then when I configure a GUI backend with matplotlib.use('TkAgg') I fall into a loop where python3 reports module not found: _tkinter and simultaneously Ubuntu reports that python3-tk is the latest version so there's nothing to do.
So I've tried
reinstalling python3
installing python3-tk then reinstalling python
pip install tk
setting up venv and doing the above
Google - but I only get the above answers. Which don't work.
So basically everything wants Tkinter but tk is there and python seems to just ignore it. I don't get it
Thank you to everyone that replied. It turns out that the solution was to recompile Python from source, telling it at configure where to look for tkinter as described in these instructions
My role at work includes automating several processes which are easily automated via Python, for use by the entire team at the office. None of my coworkers have Python set up on their devices (we're all Windows) nor would they be comfortable using it if they did, so I've been compiling my work into .exe files with tkinter.
My most recent (and, of course, the most important) program compiles into a onefile .exe without issue, and runs perfectly on my computer. My coworkers are able to open it and load files into it, but when they go to activate some of the tkinter buttons within, one coworker has absolutely nothing happen while another gets a windows error message that the program has crashed. I'm unable to recreate these issues on my side.
The .exe was made using pyinstaller in a virtual environment, in which I installed all necessary dependencies. The script does not need to read or reference any other files in order to run. Is it possible that the .exe is still somehow missing a dependency that it's finding on my device and not others? How can I be sure? Would setting it up to include an installer wizard make a difference? Thanks for the help on this!
I can't include the full script due to character limit, but I've included the opening lines with all imports.
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from scipy.fft import fft as fft
from scipy.fft import ifft as ifft
from tkinter import *
from tkinter import ttk
from tkinter import simpledialog
from tkinter import filedialog
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
Can anyone explain, why i get different results with this Code on linux and windows. On windows its a complete color picker and on Linux its Crap.
Both Tkinter Modules are Version(8.6)
from tkinter import *
from tkinter.colorchooser import *
def getColor():
color = askcolor()
print color
Button(text='Select Color', command=getColor).pack()
mainloop()
Windows Example
Linux Example
from the comments in the source code of colorchooser.py:
# this module provides an interface to the native color dialogue
# available in Tk 4.2 and newer.
You are seeing a native dialog from underlying OS, not a dialog built the usual tkinter way.
The answer to the question of why it's different on different platforms is that on both windows and the mac, the dialogs are provided by the underlying OS. On linux, tkinter must draw the dialog itself.
Good day.
I installed python 2 and python 3 in my laptop. And i'm using python 3 interpreter in writing my codes. Here is my code.
#! /usr/bin/python3
from tkinter import *
root = Tk()
theLabel = Label(root, text ="This is too easy")
theLabel.pack()
root.mainloop()
But when I double clicked the save file icon. It will say no module name tkinter. Can some one help me please?
python 2 and python 3 use tkinter in a different way.
Note: Tkinter has been renamed to tkinter in Python 3. The 2to3 tool will automatically adapt imports when converting your sources to Python 3.
The above lines are from python documentation. Not sure if python is loading tkinter using python 2 or python 3..May be internal PYTHONPATH is
messed up
Rather try this,
try:
import tkinter as tk
except ImportError:
import Tkinter as tk
Note: In these situations where you use multiple versions of same modules, try using virualenv
Virtual Env
you need to check the module name or package name before using it, do this
from Tkinter import *
On my computer with Win8.1 when I use following code
from tkinter import *
from tkinter.ttk import *
Tk().mainloop()
I see only the old looking tkinter. Any idea how to fix it?
I tried to use different theme with no result.
For some reason following code:
from tkinter import *
from tkinter.ttk import *
Style().theme_use("alt")
Tk().mainloop()
results with two identical tkinter old-fashioned windows which seems not to be correct.
PS: It seems to be issue related with Win8.1, see here:
Python 2.7 - ttk module seemingly not working in Windows 8.1
But I found no information how to fix it. Any news would be welcome. :)
EDIT1:
After trying following snippet:
from tkinter import *
from tkinter.ttk import *
from functools import partial
root = Tk()
style = Style(root)
def change(name, style):
style.theme_use(name)
for s in style.theme_names():
lb = Button(root, text=s, command=partial(change, s, style))
lb.pack()
I could say the themes are changing not only for color but completely. What is still puzzling me is I cannot see the nice fancy graphic which I expect to see.
I tried to change my windows settings for performance or to visual aspect but it is not the reason why it is not working.
If you have Win 8.1 do you have tkk working as expected?
EDIT2:
This is my look using example from
http://www.tkdocs.com/tutorial/firstexample.html
(Win7 and Win8.1)
The difference can be easily seen on ubuntu between the themes.
So, maybe it is a problem with win8.1. Considered using the ttkthemes module? The ttkthemes module is another set of more "Stylish" styles.
To install:
sudo apt-get install python3-tk
sudo -H pip3 install ttkthemes
To know more, go to:
https://github.com/RedFantom/ttkthemes/wiki/Usage
I have answered another similar question,you can refer to it through this link:
How to make pyinstaller import the ttk theme?
Lemme know in the comments if this also, doesn't work.