I'm using python tkinter to run tcl in python
And there are two ways to run a tcl command:
import tkinter
root = tkinter.Tk()
root.eval("winfo exists .l0")
root.tk.call("winfo exists .l0")
They have the same meaning
But what's different?
And if I haven't define a widget names .l0
and can I directly use
child = ".l0"
child.winfo_exists()
?
Because python told me "str has no attribute winfo_exists"
The difference is that call passes each argument to tcl as a separate word, where eval will evaluate a string by first parsing it and then executing it.
In other words, this:
root.eval("winfo exists .l0")
... is functionally identical to this:
root.tk.call("winfo", "exists", ".l0")
As for the error message 'str' object has no attribute 'winfo_exists', it means exactly that. "l0" is the name of an object in the embedded tcl interpreter, but in python "l0" is just a string and python strings don't have the attribute winfo_exists.
Related
In native visual basic within Excel, the expected behavior is
Dim AvayaReport As Object
Dim AvayaServer As Object
...
...
Set ReportInfo = AvayaServer.Reports.Reports("<Report Name>")
AvayaServer.Reports.CreateReport ReportInfo, AvayaReport
After the last line, the CreateReport method mutates AvayaReport to be a report object in this library. However, when I replicate this code within Python, AvayaReport does not get mutated.
import win32com.client
...
AvayaReport = win32com.client.dispatch("ACSREP.cvsReport")
...
ReportInfo = AvayaServer.Reports.Reports(r'<Report Name>")
AvayaServer.Reports.CreateReport(ReportInfo, AvayaReport)
I have tried to compare the behavior between the two environments and with the exception of this issue, the surrounding code is working as intended. However, this COM object has this weird implementation where it requires an empty object to be passed into it's arguments and it changes that object to reflect the created report, but I cannot figure out a way to make this work within Python.
Within VBA, the CreateReport definition is as follows:
Function CreateReport(oRepInfo, oReport, [sTimeZone as String = "default"]) As Boolean
Member of ACSTLG.cvsCatalog
What do these mean?
def f(a: {int, float}):
pass
I've seen this syntax used in some standard Python modules when fetching documentation via PyCharm, and I have no idea what it means. What's the hinted type for a in my example? What types can I pass to this function?
The particular example where I've seen this is in tkinter's Frame __init__ method, where the master parameter is of type {tk, _w}.
It's a hint telling you it wants an object with the named attributes 'int' and 'float' -- or more specifically for tkinter 'tk' and '_w'
I coded up a minimal example in pycharm:
Inpecting the python library sources -- You can see that there are attempted accesses to master.tk and master._w. That's all that pycharm was able to infer about the type of the parameter master so it floated it up to the IDE in this manner.
What is the difference between a python built-in and a normal object? we often say that in python everything is an object.
for example, when I do this in Python 3.6:
>>> import os, inspect
>>> inspect.getsource(os.scandir)
TypeError: <built-in function scandir> is not a module, class, method, function, traceback, frame, or code object
I have two questions:
is built-in function an object? if not is this why getsource throws TypeError?
why can't I find scandir in python3 documentation as a built-in?
You can't access the source of builtins and other modules that were written using the C API, since there isn't a Python source for them.
From the documentation for os.getsourcefile
Return the name of the Python source file in which an object was defined. This will fail with a TypeError if the object is a built-in module, class, or function.
I want to ask few questions about win32com.client.
What does DisPatch do? It returns COMObject wscript.shell. What exactly is this?
shell = win32com.client.Dispatch("wscript.shell")
And why do we use it while creating shortcut.
shortcutcut = shell.CreateShortcut('shortcut.lnk')
win32com.client.Dispatch creates a ActiveX/COM object. The argument is the so-called program ID. Another example of program ID is "Excel.Application" which would be used to create an instance of Excel. In your case you are creating a wscript.shell object.
The program ID is mapped to a DLL on the system by the registry. The object is instantiated through a number of well-known export methods.
Apparently, one of the methods of a wscript.shell object is CreateShortcut. Why you are using a wscript.shell to do this, I can't say.
I have a script that runs for about an hour. I run it in the python shell, and then play around with the generated variables in the shell. The problem is, I accidentally created a variable that has the same name as a function:
calcError = calcError(someVars)
so when I try to call the function again,
TypeError: 'list' object is not callable
I know that normally I can clear variables by setting them to None. But that doesn't restore the function:
calcError = None
someOtherVar = calcError(someVars)
TypeError: 'NoneType' object is not callable
How can I restore the function without having to run the script again?
If you created the function in your interactive session, then you replaced the global name with another object and you cannot get the old object back. Re-run the def calcError(...): lines you executed in your shell earlier.
If the function is imported from a module, just re-import it again to rebind the global name:
from modulename import calcError
That's because the function object is still referenced in that module's namespace; importing merely creates another reference to the same object.