Python Tkinter Attribute Error - python

I have made a tkinter program where i keep getting this error:
File "F:\Programming 2\Gui\Gui #11.py", line 78, in shape_workit
cylinder(pos=(0,0,0),axis=(1,0,0),color=self.color3.get(),
AttributeError: 'Kinter' object has no attribute 'color3'
Here is the code which the error occurs from:
def shapescolor(self):
if self.color1.get()=="Does a Orange":
color3=color.orange
if self.color1.get()=="Does a Blue":
color3=color.blue
def shape_workit(self):
try:
if self.shape.get()=="Does a Cylinder": #Creates Cylinder
cylinder(pos=(0,0,0),axis=(1,0,0),color=self.color3.get() ##ERROR HERE,
radius=float(self.radius.get()))
Here is the Code where the error it gets from

My guess is that you need to be doing self.color3 = ... rather than color3 = ..., since you're later refering to self.color3 and haven't set that attribute anywhere else in the code you posted.

Related

'TypeError: 'NoneType' object is not subscriptable' in Python while statement

I am a beginner developer just learning Python.
I'm using 'pyautogui' to create a bot that clicks a checkbox.
The code is running just the way I want it to.
But I don't know how to get out of the 'while' statement at the end.
When I click on all the checkboxes I get the following error:
TypeError: 'NoneType' object is not subscriptable
Below is the code I wrote.
import pyautogui
import PIL
pyautogui.sleep(2)
while True:
x1=pyautogui.center(pyautogui.locateOnScreen("checkbox.png", region=(50, 50, 1000, 1000), confidence=0.9))
pyautogui.moveTo(x1)
pyautogui.click()
sftp = pyautogui.locateOnScreen("sftp.png", region=(750, 450, 500, 500), confidence=0.7)
pyautogui.sleep(0.5)
print(x1)
if x1 == None:
break
print("work is done")
Execute the above code and when it's done, the output will be something like this:
Point(x=212, y=859)
Point(x=212, y=877)
Traceback (most recent call last):
File "c:\project\a_\experi.py", line 7, in <module>
x1=pyautogui.center(pyautogui.locateOnScreen("checkbox.png", region=(50, 50, 1000, 1000), confidence=0.9))
File "c:\python39-32\lib\site-packages\pyscreeze\__init__.py", line 581, in center
return Point(coords[0] + int(coords[2] / 2), coords[1] + int(coords[3] / 2))
TypeError: 'NoneType' object is not subscriptable
From the official documentation of PyAutoGUI, we can see that the locateOnScreen() function raises a ImageNotFoundException and returns None when it is unable to locate the image it is searching for. This error might occur due to the following reasons:
If there is an error in the image file name (has to be the same with the original image file name).
If there is an error in the extension of the image file (has to be the same with the original image extension).
If the pixels specified for the image are not matching. Generally, the pixels (region) should be a very close match to the actual image position. A less value for the confidence parameter can be tried to overlook negligible differences between the actual image position and the pixels specified.

Getting error message: AttributeError: 'list' object has no attribute 'lower' in Python

I have the problem, that I am trying to run a function which has as input an array of strings and before making some if-statement I am lowering the strings. somehow i am receiving the error message
AttributeError: 'list' object has no attribute 'lower'
It is not clear for me why do I receive it. The funny part is, If I have both in one script it works. If I source the function out and load it into another script it does not work. The code looks like the following:
def Calc(String_Array1, String_Array2):
String_Array1 = String_Array1.lower()
String_Array2 = String_Array2.lower()
if String_Array1 == "a":
print(String_Array1)
elif String_Array2 == "b":
print(String_Array2)
else:
raise Exception("Something went wrong")
return 0
I am running then my own module like the following
import helper_module as hm
String_Array1 = ["A"]
String_Array2 = ["B"]
test = hm.CalcFXFwdCurve(String_Array1,String_Array2)
print(test)
So the solution works with an numpy array using char.lower(). The the if-statement can be performed using any() or all()

TypeError: 'NoneType' object is not callable when object referenced the second time

I am a total at writing code let alone python. I am facing this error with the following code.
filename is abcd.py
class device():
def login(self,ip):
self.tn= telnetlib.Telnet(ip)
self.tn.read_until("login: ")
self.tn.write(login)
def sendcommand(self,command):
self.sendcommand= self.tn.write(command)
This python code is imported by another file.
from abcd import *
def foo():
ip = 'ip address'
dev1 = switch()
dev1.login(ip)
dev1.sendcommand('cmd1')
dev1.sendcommand('cmd2')
foo()
When I call the foo function everything executes correctly till we reach dev1.sendcommand('cmd2'). The error received is
dev1.sendcommand('cmd2')
TypeError: 'NoneType' object is not callable
I have simply no clue why its happening. Am I modifying the object in some way?
Yes. When you do self.sendcommand= self.tn.write(command), you overwrite the method sendcommand with the value of self.tn.write(command). Use a different name for the variable than for the method.
The issue seems to be in the line -
def sendcommand(self,command):
self.sendcommand= self.tn.write(command)
You are setting the return value of your writer() to self.sendcommand , which is overwriting the function , you should not do that , just call the function , without setting the return value anywhere . Example -
def sendcommand(self,command):
self.tn.write(command)

Entry().get() doesn't work in Python 3.4

Despite all attempts, I cannot seem to get Entry().get() to assign a string in an Entry window. Here's a code snippet:
Itx_bcn_ent = Entry(win).grid(row=1,column=1)
I define a button to call a function:
btn = Button(win,text="Run",command=getnums).grid(row=5,column=3,padx=100)
Here's the function:
def getnums():
Itx_bcn = Itx_bcn_ent.get()
When I run the script, I get the following error:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python34\lib\tkinter\__init__.py", line 1482, in __call__
return self.func(*args)
File "C:\Python34\voltage_substate_GUI.py", line 7, in getnums
Itx_bcn = Itx_bcn_ent.get()
AttributeError: 'NoneType' object has no attribute 'get'
I've seen the construct to use a Class StringVar() and the option "textvariable=" with the Entry() object, however doing this seems overly complicated as it just creates an additional set of variables between what's in the Entry window and the variable I am trying to assign.
Any thoughts on this?
Entry.grid() returns None, so when you do something = Entry(root).grid(), you're getting something=None.
This isn't a problem until you try to use that thing! That's why you're getting a 'NoneType' object has no attribute 'get' error.
Itx_bcn_ent = Entry(win)
Itx_bcn_ent.grid(row=1,column=1)
Now your button works :), though you have the same problem with your btn = line. I've yet to have to reuse that assignment, so maybe just drop it?
Button(win,text="Run",command=getnums).grid(row=5,column=3,padx=100)

Error while calling a function from one module to another module in python

i have written two modules in python "Sample.py" and "GenericFunctions.py" from "Sample.py", iam calling a function
present in "GenericFunctions.py" but i am not able to call that function getting error as "AttributeError: 'module' object has no attribute 'fn_ElseLog'"
Code in Sample.py:
import GenericFunctions
def sampleee():
g_TCaseID="SD1233"
g_TCDescription="Login"
g_TestData="hi"
g_Result="Fail"
g_Remarks="testing"
g_TargetEnvironment="S1"
g_TargetSystem="Legacy"
g_TargetRegion="EU"
x = GenericFunctions.fn_ElseLog(g_TCaseID, g_TCDescription, g_TestData, g_Result, g_Remarks)
sampleee()
Code in GenericFunctions.py:
def fn_ElseLog(g_TCaseID, g_TCDescription, g_TestData, g_Result, g_Remarks):
print "entered in ElseLog Function"
Output= fn_Output(g_TCaseID, g_TCDescription, g_TestData, g_Result , g_Remarks)
print ("Testcase"+"'"+g_TCDescription+"'"+"execution completed"+"'"+g_TargetEnvironment+"-"+g_TargetRegion)
def fn_Output(p_TCaseID, p_TCDescription, p_TestData, p_Result , p_Remarks):
OutputSheet=""
OutputSheet="\Test"+"_"+g_TargetEnvironment+"_"+g_TargetSystem+"_"+g_TargetRegion+".xlsx"
OutputPath=r"C:\Users\u304080\Desktop\PlayAround\Shakedowns\OutputResultFiles"
#objExcel1 = win32.gencache.EnsureDispatch('Excel.Application')
Outputfile=os.path.exists(OutputPath+OutputSheet)
if Outputfile==True :
print('Output file is present')
else:
print('Output file is not present')
return
objExceloutput = win32.gencache.EnsureDispatch('Excel.Application')
#excel.DisplayAlerts = False
objoutputworkbook = objExceloutput.Workbooks.Open(OutputPath+OutputSheet)
objSheetOutput = objoutputworkbook.Sheets(1)
OutputRowCount =objSheetOutput.UsedRange.Rows.Count
print "OutputRowcount" , OutputRowCount
objSheetOutput.Cells(OutputRowCount+1,1).Value=p_TCaseID
objSheetOutput.Cells(OutputRowCount+1,2).Value=p_TCDescription
objSheetOutput.Cells(OutputRowCount+1,3).Value=p_TestData
objSheetOutput.Cells(OutputRowCount+1,4).Value=p_Result
objSheetOutput.Cells(OutputRowCount+1,4).Font.Bold = True
if p_Result=="Pass":
objSheetOutput.Cells(OutputRowCount+1,1).Font.ColorIndex = 10
else:
objSheetOutput.Cells(OutputRowCount+1,1).Font.ColorIndex = 3
objoutputworkbook.SaveAs(OutputPath)
objSheetOutput=None
objoutputworkbook=None
objExceloutput.Quit()
objExceloutput=None
Can you guys show me a solution for this?
Try:
delete all pyc files in your directory. (to ensure it is not in cached)
print(dir(GenericFunctions)) and print(GenericFunctions.__file__) in Sample.py, to see if you are importing the file you think you are importing.
see if there are other files called GenericFunctions somewhere on your PYTHONPATH (echo $PYTHONPATH).
present in "GenericFunctions.py" but i am not able to call that
function getting error as "AttributeError: 'module' object has
no attribute 'fn_ElseLog'"
As presented, your code looks correct.
Check to make sure both files are in the same directory, then either restart your python or do a reload(GenericFunctions) to make sure you don't have an out-of-date version in the sys.modules cache.

Categories

Resources