Cannot seem to figure out the below Tkinter message. Usually doing a quick
search will provide answers but this time I seem to miffed the search engines as to
what might be causing the below error. Curious to know if I am missing a Python package or line 25 below is used in an older version of Python and it has been updated to
a newer command.
I am importing the following packages into the script:
from tkinter import *
from tkinter import filedialog
The function is suppose to save any typed text put into a text area. It does save the file but the file is empty.
Thanks,
Kurt
C:\Users\kurt>python --version
Python 3.10.4
def saveFiles():
filename = filedialog.asksaveasfile(
mode='w',
title="Save a File",
defaultextension=".txt"
)
filename.config(mode='w') ------------> **This is line 25**
pathh.insert(END, filename)
data = str(txtarea.get(1.0, END))
filename.write(data)
filename.close()
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.1264.0_x64__qbz5n2kfra8p0\lib\tkinter\__init__.py", line 1921, in __call__
return self.func(*args)
File "c:\Users\kurt\Documents\Scripts\TKinter\fileExplorerReadFile.py", line 25, in saveFiles
filename.config(mode='w')
AttributeError: '_io.TextIOWrapper' object has no attribute 'config'
The error is saying that the object returned by the asksaveasfile method doesn't have a config method.
tkinter.filedialog.asksaveasfile(mode='w', **options)ΒΆ
Create a SaveAs dialog and return a file object opened in write-only mode.
When you call the asksaveasfile method, it automatically returns a file object in write mode already so there is no need for any further configuration to write to the file. If you were to omit the line throwing the error, your code should work the way you intended.
Related
I have been using the python shelf module to store face encodings from the python face-recognition module below. I did this to make the live image recognition process faster.
I then imported these encodings in another script using the shelf module again, assigned them to a variable, and use them further down my script. This all works fine in the python idle environment and when I run it from the terminal. However, on startup, the shelf module fails to import the data. Can anyone tell me why this happens at start-up? The error I get on the log file is below. I have been stuck on it for a few days now. Is there a better way of storing and reusing the encodings? Thanks in advance.
the bit of code that fails on start-up but runs fine otherwise
import shelve
shelfFile = shelve.open('face_encodings')
known_encodings = shelfFile['known_encodings']
known_names = shelfFile['known_names']
shelfFile.close()
the error on startup
Traceback (most recent call last): File "/usr/lib/python3.7/shelve.py", line 111, in __getitem__
value = self.cache[key]
KeyError: 'known_encodings'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/pi/Desktop/run/testing.py", line 4, in <module>
known_encodings = shelfFile['known_encodings']
File "/usr/lib/python3.7/shelve.py", line 113, in __getitem__
f = BytesIO(self.dict[key.encode(self.keyencoding)])
KeyError: b'known_encodings'
face-recognition module
https://pypi.org/project/face-recognition/
I recently tried to automate a windows application using a pywinauto since it enables automation with using python. I just started it and encountered a problem which hinders me to continue.
I get this error whenever I try to find the relevant element:
Traceback (most recent call last):
File "test.py", line 14, in <module>
app.findwindows.find_elements().click_input()
File "C:\Users\Bar\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pywinauto\application.py", line 173, in __call__
format(self.criteria[-1]['best_match']))
AttributeError: WindowSpecification class has no 'find_elements' method
This is my code:
from pywinauto.application import Application
import pywinauto
import time
app = Application(backend='uia').start(r"C:\Program
Files\Intellech\Analyzer\Suite.exe")
time.sleep(3)
app.findwindows.find_windows(auto_id='btQuick').click_input()
Can you help me to find out the reason of this error?
findwindows is a module name, not an attribute of Application object. This is correct code for the last line:
app.window(title="Your Main Window").child_window(auto_id='btQuick').click_input()
where "Your Main Window" should be changed to correct text of the top level window.
Okay, first of all I don't know what is the actual problem here so I couldn't come up with a more accurate title. Maybe some of you can edit it to make it accurate
The following is the minimalised code to reproduce the problem I'm having.
from traybar import SysTrayIcon
from cal import Calendar
import Tkinter
class Add():
def __init__(self,master):
Calendar(master).pack()
def add(systray):
root = Tkinter.Tk()
Add(root)
root.mainloop()
SysTrayIcon("abc.ico","abc", (('Add',None, add), ) ,default_menu_index=0).start()
The cal and the trabar are these files http://tkinter.unpythonic.net/wiki/TkTableCalendar and https://github.com/Infinidat/infi.systray/blob/develop/src/infi/systray/traybar.py respectively.
If you run this, it will make a icon in the system tray of a windows machine with the options Add and Quit. clicking on the app opens up the calender, no problem. Close the calender and click on the Add again. But this time it doesn't open the calendar and throws the following error
`
Traceback (most recent call last):
File "_ctypes/callbacks.c", line 314, in 'calling callback function'
File "C:\Users\Koushik Naskar\AppData\Roaming\Python\Python27\site-packages\traybar.py", line 79, in WndProc
self._message_dict[msg](hwnd, msg, wparam.value, lparam.value)
File "C:\Users\Koushik Naskar\AppData\Roaming\Python\Python27\site-packages\traybar.py", line 276, in _command
self._execute_menu_option(id)
File "C:\Users\Koushik Naskar\AppData\Roaming\Python\Python27\site-packages\traybar.py", line 283, in _execute_menu_option
menu_action(self)
File "C:\Users\Koushik Naskar\Desktop\So\temp.py", line 11, in add
Add(root)
File "C:\Users\Koushik Naskar\Desktop\So\temp.py", line 7, in __init__
Calendar(master).pack()
File "C:\Users\Koushik Naskar\Desktop\So\cal.py", line 66, in __init__
state='disabled', browsecommand=self._set_selection)
File "C:\Python27\lib\lib-tk\tktable.py", line 118, in __init__
Tkinter.Widget.__init__(self, master, 'table', kw)
File "C:\Python27\lib\lib-tk\Tkinter.py", line 2090, in __init__
(widgetName, self._w) + extra + self._options(cnf))
_tkinter.TclError: invalid command name "table"
`
This problem only appears when I use SysTrayIcon with the Calendar.Instead of Calendar if you use simple Tkinter Button or Label etc. this error doesn't appear. Also I can use the Calendar widget normally (without the SysTrayIcon ) in a usual Tkinter GUI as many times as I want, no error occur there. Now I don't have any clue about whats happenning here and how to fix this. What problem does SysTrayIcon have with Calendar and Why the error doesn't happen the first time I open the GUI? Please help.
TkTableCalendar requies the tktable module, which you have installed in lib-tk (3rd party modules usually go into lib/site-packages) as indicated by this part of the traceback.
File "C:\Python27\lib\lib-tk\tktable.py", line 118, in __init__
Tkinter.Widget.__init__(self, master, 'table', kw)
The tktable module requires that your tcl/tk installation have the tktable extension. That extension defines the 'table' widget. It is not part of the standard tcl/tk that is installed with Python on Windows. Hence
_tkinter.TclError: invalid command name "table"
The tktable source code (probably a mixture of tcl and C) is hosted at SourceForge. This page says that it is part of the ActiveState Batteries Included distribution. I don't know if the free version of AS tcl/tk has all the 'batteries'. And I don't know how to replace the tcl/tk installation you already have with a new one. I personally would use an alternative if possible.
I'm developing a desktop app which uses the Mayavi package. So, I'm using anaconda with mayavi 4.4.0 and pyqt 4.11.3 (python 2.7). When I try to save the image using the Mayavi interface I get:
KGlobal::locale(): Warning your global KLocale is being recreated with a valid main component instead of a fake component, this usually means you tried to call i18n related functions before your main component was created. You should not do that since it most likely will not work
Object::connect: No such signal org::freedesktop::UPower::DeviceAdded(QDBusObjectPath)
Object::connect: No such signal org::freedesktop::UPower::DeviceRemoved(QDBusObjectPath)
kfilemodule(5709): couldn't create slave: "Unable to create io-slave:
klauncher said: Unknown protocol ''.
"
Traceback (most recent call last):
File "/home/facu/anaconda/lib/python2.7/site-packages/pyface/ui/qt4/action/action_item.py", line 358, in _qt4_on_triggered
action.perform(action_event)
File "/home/facu/anaconda/lib/python2.7/site-packages/pyface/action/action.py", line 100, in perform
self.on_perform()
File "/home/facu/anaconda/lib/python2.7/site-packages/tvtk/pyface/ui/qt4/decorated_scene.py", line 159, in _save_snapshot
if dialog.open() == OK:
File "/home/facu/anaconda/lib/python2.7/site-packages/pyface/i_dialog.py", line 122, in open
self.close()
File "/home/facu/anaconda/lib/python2.7/site-packages/pyface/ui/qt4/file_dialog.py", line 103, in close
self.wildcard_index = self.control.nameFilters().index(
AttributeError: 'QStringList' object has no attribute 'index'
I have fixed others issues editing *.py files, I don't know how to fix this one in particular .
I've scoured the internet on this one and haven't come up with anything, so please excuse me if this is a dumb question, but...
I've written a Tkinter class that takes a .xlsx file as input for one of its processes. You press a button and it uses TKFileDialog.askopenfile() to pop the familiar-looking file input dialog. However, if a user hits cancel, the input is NoneType, the file doesn't open, and the TKinter window terminates with a TypeError.
def GUIDocPullRename(self):
indir = self.e1.get()
Tab = askopenfile()
try:
tBook = xlrd.open_workbook(Tab)
except TypeError:
self.text.insert(END, "Canceled")
#Insert method-ender here
I want to insert something in the exception catch that stops the GUIDocPullRename from continuing, but allows the parent TKinter process to continue running so the user can specify another file without having to re-run the whole program. Is it possible or am I just being a silly newb?
Error is this, when the try/except is excluded:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python27\lib\lib-tk\Tkinter.py", line 1470, in __call__
return self.func(*args)
File "P:/Administrative/IT/Scripting/BillsPython/PdfMerger.py", line 102, in GUIDocPullRename
tBook = xlrd.open_workbook(Tab)
File "C:\Python27\lib\site-packages\xlrd\__init__.py", line 394, in open_workbook
f = open(filename, "rb")
TypeError: coercing to Unicode: need string or buffer, NoneType found